KEMBAR78
Socket Programming with Python | PDF
www.glcnetworks.com
Socket
programming
with python
GLC Webinar,
07 oct 2021
Achmad Mardiansyah
achmad@glcnetworks.com
GLC Networks, Indonesia
1
Image source: morioh.com
www.glcnetworks.com
Agenda
● Introduction
● Review prerequisite knowledge
● Python networking and socket programming
● Protocol analyzer
● Live practice
● Q & A
2
www.glcnetworks.com
introduction
3
www.glcnetworks.com
What is GLC?
● Garda Lintas Cakrawala (www.glcnetworks.com)
● Based in Bandung, Indonesia
● Areas: Training, IT Consulting
● Certified partner for: Mikrotik, Ubiquity, Linux foundation
● Product: GLC radius manager
● Regular event
4
www.glcnetworks.com
Trainer Introduction
● Name: Achmad Mardiansyah
● Base: bandung, Indonesia
● Linux user since 1999, mikrotik user since 2007, UBNT
2011
● Mikrotik Certified Trainer
(MTCNA/RE/WE/UME/INE/TCE/IPv6)
● Mikrotik/Linux Certified Consultant
● Website contributor: achmadjournal.com, mikrotik.tips,
asysadmin.tips
● More info:
http://au.linkedin.com/in/achmadmardiansyah
5
www.glcnetworks.com
Past experience
6
● 2021 (Congo DRC, Malaysia): network support,
radius/billing integration
● 2020 (Congo DRC, Malaysia): IOT integration,
network automation
● 2019, Congo (DRC): build a wireless ISP from
ground-up
● 2018, Malaysia: network revamp, develop billing
solution and integration, setup dynamic routing
● 2017, Libya (north africa): remote wireless migration
for a new Wireless ISP
● 2016, United Kingdom: workshop for wireless ISP,
migrating a bridged to routed network
www.glcnetworks.com
About GLC webinar?
● First webinar: january 1, 2010 (title:
tahun baru bersama solaris - new year
with solaris OS)
● As a sharing event with various topics:
linux, networking, wireless, database,
programming, etc
● Regular schedule
● Irregular schedule: as needed
● Checking schedule:
http://www.glcnetworks.com/schedule
● You are invited to be a presenter
○ No need to be an expert
○ This is a forum for sharing: knowledge,
experiences, information
7
www.glcnetworks.com
Please introduce yourself
● Your name
● Your company/university?
● Your networking experience?
● Your mikrotik experience?
● Your expectation from this course?
8
www.glcnetworks.com
Prerequisite
● This presentation some prerequisite knowledge
● We assume you already understand:
○ How computer network works
○ Encapsulation process
○ Python programming
○ Linux skill
○
9
www.glcnetworks.com
Review prerequisite knowledge
10
www.glcnetworks.com
Basic stuffs
● Variable
● Command (keywords)
● Operation (+, -, *, /, %)
● Array (list, tuple, dictionary)
● Loop
○ For loop
○ While loop
11
x = "Python is "
y = "awesome"
z = x + y
print(z)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
i = 1
while i < 6:
print(i)
i += 1
Source: www.w3schools.com
www.glcnetworks.com
Procedural Python
● Code executed sequentially
● Easy to understand
● Faster to implement
● Natural
● Program lines can be very long
● Need a way to architect to:
○ Manage our code physically
○ Manage our application logic
12
x = "Python is "
y = "awesome"
z = x + y
print(z)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Source: www.w3schools.com
www.glcnetworks.com
Efficient code: using functions
● A function is a block of code which
only runs when it is called.
● You can pass data, known as
parameters, into a function.
● A function can return data as a
result.
13
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
Source: www.w3schools.com
www.glcnetworks.com
Efficient code: using external functions (library)
● Consider a module to be the same
as a code library.
● A file containing a set of functions
you want to include in your
application.
14
vim mymodule.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
vim myapp.py
import mymodule
a = mymodule.person1["age"]
print(a)
Source: www.w3schools.com
www.glcnetworks.com
Exception
● Exception is a feature to handle
the an error so that it wont
displayed on foreground
● Use try … except
● Python built-in exception:
https://docs.python.org/3/library/ex
ceptions.html
●
15
vim file.py
try:
a=5
b=0
print (a/b)
except TypeError:
print('Unsupported operation')
except ZeroDivisionError:
print ('Division by zero not allowed')
print ('Out of try except blocks')
Source: www.w3schools.com
www.glcnetworks.com
7 OSI layer & protocol
16
● OSI layer Is a conceptual model from ISO
(International Standard Organization) for project
OSI (Open System Interconnection)
● When you send a message with a courier, you
need to add more info to get your message arrived
at the destination (This process is called
encapsulation)
● What is protocol
○ Is a set of rules for communication
○ Available on each layer
● Communication consist of series encapsulation
○ SDU: service data unit (before PDU)
○ PDU: protocol data unit (after header is added)
www.glcnetworks.com
Layered model (TCP/IP vs ISO) and encapsulation
17
/ datagram
www.glcnetworks.com
7 OSI layer & protocol
18
● OSI layer Is a conceptual model from ISO
(International Standard Organization) for project
OSI (Open System Interconnection)
● When you send a message with a courier, you
need to add more info to get your message arrived
at the destination (This process is called
encapsulation)
● What is protocol
○ Is a set of rules for communication
○ Available on each layer
● Communication consist of series encapsulation
○ SDU: service data unit (before PDU)
○ PDU: protocol data unit (after header is added)
www.glcnetworks.com
Layered model (TCP/IP vs ISO) and encapsulation
19
/ datagram
www.glcnetworks.com
Layer 4 header (which one is TCP?)
20
www.glcnetworks.com
Layer 3 header (which one is IPv4?)
21
www.glcnetworks.com
Ethernet header (which is the MTU?)
22
www.glcnetworks.com
802.11 header
23
www.glcnetworks.com
Did you notice?
● There is a big overhead on encapsulation process
● More encapsulation means less payload?
24
www.glcnetworks.com
Socket programming
25
www.glcnetworks.com
What is socket (networking)
● software/app need to be able to communicate to each other (IPC - inter
process communication)
● IPC method:
○ Pipe (e.g UNIX)
○ Semaphore (e.g. unix)
○ Socket (famous in networking)
● Socket method:
○ UNIX socket
○ STREAM socket (internet)
● STREAM socket:
○ Datalink
○ Internet (IPv4 / IPv6)
○ Transport (TCP / UDP)
26
www.glcnetworks.com
What socket programming can do?
● Server app
● Multiplayer online games
● Chat application
● Data gathering
● Internet of Things (IOT)
● Create your own protocol / application
●
27
www.glcnetworks.com
Python socket programming
● Module: socket, sys
● Server side
○ Setup ip address and port
○ Bind / listening on a port/ip address
● Client side
○ Connect to ip address / port
● Example code:
https://github.com/amardiansyah/python_socket_programming
●
28
www.glcnetworks.com
troubleshooting
29
www.glcnetworks.com
Protocol analyzer is your friend
30
www.glcnetworks.com
LIVE practice
31
www.glcnetworks.com
preparation
● SSH client
● SSH parameters
○ SSH address
○ SSH port
○ SSH username
○ SSH password
32
www.glcnetworks.com
Q & A
33
www.glcnetworks.com
Interested? Just come to our training...
● Topics are arranged in systematic and logical way
● You will learn from experienced teacher
● Not only learn the materials, but also sharing experiences, best-practices, and
networking
34
www.glcnetworks.com
End of slides
● Thank you for your attention
● Please submit your feedback: http://bit.ly/glcfeedback
● Find our further event on our website : https://www.glcnetworks.com/en/
● Like our facebook page: https://www.facebook.com/glcnetworks
● Slide: https://www.slideshare.net/glcnetworks/
● Discord (bahasa indonesia): (https://discord.gg/6MZ3KUHHBX)
● Recording (youtube): https://www.youtube.com/c/GLCNetworks
● Stay tune with our schedule
● Any questions?
35

Socket Programming with Python

  • 1.
    www.glcnetworks.com Socket programming with python GLC Webinar, 07oct 2021 Achmad Mardiansyah achmad@glcnetworks.com GLC Networks, Indonesia 1 Image source: morioh.com
  • 2.
    www.glcnetworks.com Agenda ● Introduction ● Reviewprerequisite knowledge ● Python networking and socket programming ● Protocol analyzer ● Live practice ● Q & A 2
  • 3.
  • 4.
    www.glcnetworks.com What is GLC? ●Garda Lintas Cakrawala (www.glcnetworks.com) ● Based in Bandung, Indonesia ● Areas: Training, IT Consulting ● Certified partner for: Mikrotik, Ubiquity, Linux foundation ● Product: GLC radius manager ● Regular event 4
  • 5.
    www.glcnetworks.com Trainer Introduction ● Name:Achmad Mardiansyah ● Base: bandung, Indonesia ● Linux user since 1999, mikrotik user since 2007, UBNT 2011 ● Mikrotik Certified Trainer (MTCNA/RE/WE/UME/INE/TCE/IPv6) ● Mikrotik/Linux Certified Consultant ● Website contributor: achmadjournal.com, mikrotik.tips, asysadmin.tips ● More info: http://au.linkedin.com/in/achmadmardiansyah 5
  • 6.
    www.glcnetworks.com Past experience 6 ● 2021(Congo DRC, Malaysia): network support, radius/billing integration ● 2020 (Congo DRC, Malaysia): IOT integration, network automation ● 2019, Congo (DRC): build a wireless ISP from ground-up ● 2018, Malaysia: network revamp, develop billing solution and integration, setup dynamic routing ● 2017, Libya (north africa): remote wireless migration for a new Wireless ISP ● 2016, United Kingdom: workshop for wireless ISP, migrating a bridged to routed network
  • 7.
    www.glcnetworks.com About GLC webinar? ●First webinar: january 1, 2010 (title: tahun baru bersama solaris - new year with solaris OS) ● As a sharing event with various topics: linux, networking, wireless, database, programming, etc ● Regular schedule ● Irregular schedule: as needed ● Checking schedule: http://www.glcnetworks.com/schedule ● You are invited to be a presenter ○ No need to be an expert ○ This is a forum for sharing: knowledge, experiences, information 7
  • 8.
    www.glcnetworks.com Please introduce yourself ●Your name ● Your company/university? ● Your networking experience? ● Your mikrotik experience? ● Your expectation from this course? 8
  • 9.
    www.glcnetworks.com Prerequisite ● This presentationsome prerequisite knowledge ● We assume you already understand: ○ How computer network works ○ Encapsulation process ○ Python programming ○ Linux skill ○ 9
  • 10.
  • 11.
    www.glcnetworks.com Basic stuffs ● Variable ●Command (keywords) ● Operation (+, -, *, /, %) ● Array (list, tuple, dictionary) ● Loop ○ For loop ○ While loop 11 x = "Python is " y = "awesome" z = x + y print(z) fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) i = 1 while i < 6: print(i) i += 1 Source: www.w3schools.com
  • 12.
    www.glcnetworks.com Procedural Python ● Codeexecuted sequentially ● Easy to understand ● Faster to implement ● Natural ● Program lines can be very long ● Need a way to architect to: ○ Manage our code physically ○ Manage our application logic 12 x = "Python is " y = "awesome" z = x + y print(z) fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) Source: www.w3schools.com
  • 13.
    www.glcnetworks.com Efficient code: usingfunctions ● A function is a block of code which only runs when it is called. ● You can pass data, known as parameters, into a function. ● A function can return data as a result. 13 def my_function(fname, lname): print(fname + " " + lname) my_function("Emil", "Refsnes") Source: www.w3schools.com
  • 14.
    www.glcnetworks.com Efficient code: usingexternal functions (library) ● Consider a module to be the same as a code library. ● A file containing a set of functions you want to include in your application. 14 vim mymodule.py person1 = { "name": "John", "age": 36, "country": "Norway" } vim myapp.py import mymodule a = mymodule.person1["age"] print(a) Source: www.w3schools.com
  • 15.
    www.glcnetworks.com Exception ● Exception isa feature to handle the an error so that it wont displayed on foreground ● Use try … except ● Python built-in exception: https://docs.python.org/3/library/ex ceptions.html ● 15 vim file.py try: a=5 b=0 print (a/b) except TypeError: print('Unsupported operation') except ZeroDivisionError: print ('Division by zero not allowed') print ('Out of try except blocks') Source: www.w3schools.com
  • 16.
    www.glcnetworks.com 7 OSI layer& protocol 16 ● OSI layer Is a conceptual model from ISO (International Standard Organization) for project OSI (Open System Interconnection) ● When you send a message with a courier, you need to add more info to get your message arrived at the destination (This process is called encapsulation) ● What is protocol ○ Is a set of rules for communication ○ Available on each layer ● Communication consist of series encapsulation ○ SDU: service data unit (before PDU) ○ PDU: protocol data unit (after header is added)
  • 17.
    www.glcnetworks.com Layered model (TCP/IPvs ISO) and encapsulation 17 / datagram
  • 18.
    www.glcnetworks.com 7 OSI layer& protocol 18 ● OSI layer Is a conceptual model from ISO (International Standard Organization) for project OSI (Open System Interconnection) ● When you send a message with a courier, you need to add more info to get your message arrived at the destination (This process is called encapsulation) ● What is protocol ○ Is a set of rules for communication ○ Available on each layer ● Communication consist of series encapsulation ○ SDU: service data unit (before PDU) ○ PDU: protocol data unit (after header is added)
  • 19.
    www.glcnetworks.com Layered model (TCP/IPvs ISO) and encapsulation 19 / datagram
  • 20.
    www.glcnetworks.com Layer 4 header(which one is TCP?) 20
  • 21.
    www.glcnetworks.com Layer 3 header(which one is IPv4?) 21
  • 22.
  • 23.
  • 24.
    www.glcnetworks.com Did you notice? ●There is a big overhead on encapsulation process ● More encapsulation means less payload? 24
  • 25.
  • 26.
    www.glcnetworks.com What is socket(networking) ● software/app need to be able to communicate to each other (IPC - inter process communication) ● IPC method: ○ Pipe (e.g UNIX) ○ Semaphore (e.g. unix) ○ Socket (famous in networking) ● Socket method: ○ UNIX socket ○ STREAM socket (internet) ● STREAM socket: ○ Datalink ○ Internet (IPv4 / IPv6) ○ Transport (TCP / UDP) 26
  • 27.
    www.glcnetworks.com What socket programmingcan do? ● Server app ● Multiplayer online games ● Chat application ● Data gathering ● Internet of Things (IOT) ● Create your own protocol / application ● 27
  • 28.
    www.glcnetworks.com Python socket programming ●Module: socket, sys ● Server side ○ Setup ip address and port ○ Bind / listening on a port/ip address ● Client side ○ Connect to ip address / port ● Example code: https://github.com/amardiansyah/python_socket_programming ● 28
  • 29.
  • 30.
  • 31.
  • 32.
    www.glcnetworks.com preparation ● SSH client ●SSH parameters ○ SSH address ○ SSH port ○ SSH username ○ SSH password 32
  • 33.
  • 34.
    www.glcnetworks.com Interested? Just cometo our training... ● Topics are arranged in systematic and logical way ● You will learn from experienced teacher ● Not only learn the materials, but also sharing experiences, best-practices, and networking 34
  • 35.
    www.glcnetworks.com End of slides ●Thank you for your attention ● Please submit your feedback: http://bit.ly/glcfeedback ● Find our further event on our website : https://www.glcnetworks.com/en/ ● Like our facebook page: https://www.facebook.com/glcnetworks ● Slide: https://www.slideshare.net/glcnetworks/ ● Discord (bahasa indonesia): (https://discord.gg/6MZ3KUHHBX) ● Recording (youtube): https://www.youtube.com/c/GLCNetworks ● Stay tune with our schedule ● Any questions? 35