SlideShare ist ein Scribd-Unternehmen logo
1 von 27
NETWORK PROGRAMMING
USING PYTHON
ALI NEZHAD
OUTLINE
• Data Communications Fundamentals
• Sockets
• Python support for sockets
• TCP and UDP communication examples
• Higher level programming in Python
2
TCP/IP MODEL
NETWORK
Client
ServerRequest
Response
3
DATA ENCAPSULATION
4
SSH
SMTP
FTP
TRANSPORT LAYER
• Provides service to
application layer.
• Multiplexes applications.
• TCP does much more.
• Most applications use TCP.
• UDP for real-time or tolerant
applications.
TCP 5
Source: Data and Computer Communications (Stallings)
TCP OPERATION
6
Source: Data and Computer Communications (Stallings)
TCP VS. UDP
7
Source: Data and Computer Communications (Stallings)
IP PACKET
8
Source: Data and Computer Communications (Stallings)
SOCKETS
• Endpoints of a bidirectional communication channel
• Used to address an application on a machine.
• Allow multiplexing of applications.
• Identified by combination: IP_address + protocol + port_no.
• Servers use the well-known port numbers (0 - 1023).
• Clients use the ephemeral port numbers (1024 - 65535).
• Can be treated as files.
9
SOCKET MODULE IN PYTHON
• Used in low-level network programming
• Usually used by higher level APIs.
• Functions correspond to UNIX system calls.
• Except makefile()
• Examples: socket(), bind(), listen(), accept(), connect(), send(), recv(), close()
• Attributes
• family, type, proto
• Used to create socket objects.
• s = socket.socket( family, type, proto) 10
A subtype, usually
0.
SOCKET ATTRIBUTES
ADDRESS FAMILY
• Identifies the domain and a family of protocols
• Options for the address family of the socket:
• AF_UNIX: a single string for UNIX
• Represented by socket.AF_UNIX constant
• Addres is just a filename.
• AF_INET: a pair of (host, port) for IPv4
• Represented by socket.AF_INET constant
• (‘www.yahoo.com’, 80)
• (‘209.191.88.254’, 80)
• AF_INET6: quadtuple of (host, port, flowid, scopeid) for IPv6
• Represented by socket.AF_INET6 constant 11
SOCKET ATTRIBUTES
TYPE
• Commonly used types:
• socket.SOCK_STREAM (for TCP sockets, connection-oriented)
• socket.SOCK_DGRAM (for UDP sockets, connectionless)
12
SERVER
TCP
13
import socket
HOST = ‘ ' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Source: docs.python.org
Would be 80
for a web
server.
SERVER (CONTINUED)
TCP
14
import socket
HOST = ‘ ' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Source: docs.python.org
SERVER (CONTINUED)
TCP
15
import socket
HOST = ‘ ' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Source: docs.python.org
SERVER (CONTINUED)
TCP
16
import socket
HOST = ‘ ' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Source: docs.python.org
SERVER (CONTINUED)
TCP
17
import socket
HOST = ‘ ' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Source: docs.python.org
SERVER (CONTINUED)
TCP
18
import socket
HOST = ‘ ' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Source: docs.python.org
CLIENT
TCP
import socket
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
19
Source: docs.python.org
No need to
specify local
socket.
CLIENT (CONTINUED)
TCP
import socket
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
20
Source: docs.python.org
Could be an HTTP request like
(‘GET /index.html HTTP/1.0
nn ’).
Transport layer gives service to
application layer.
SERVER
UDP
21
Source: www.dabeas.com
CLIENT
UDP
22
Source: www.dabeas.com
HIGH LEVEL NETWORK PROGRAMMING
• Support for specific applications e.g. HTTP, FTP, …
• Instead of socket module, servers use other modules and libraries, e.g.:
• HTTP: httplib, urllib2, xmllib
• FTP: ftplib, urllib
• SMTP: smtplib
• Servers usually use the SocketServer module.
• Different file formats and encodings must be handled at the client using
various libraries such as csv, htmlparser and json. 23
SOCKETSERVER EXAMPLE
TIME SERVER
24
import SocketServer
import time
Class TimeHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.request.sendall(time.ctime()+"n")
serv = SocketServer.TCPServer(("",8000),TimeHandler)
serv.serve_forever()
Source: www.dabeas.com
APPLICATION LAYER
HTTP EXAMPLE
• Opening a web page:
25
import urllib2
u = urllib2.urlopen("http://www.python/org/index.html")
data = u.read()
print data
SUMMARY
• Client/server communication model
• TCP and UDP give service to applications.
• Use TCP sockets if you need reliability.
• A socket identifies a single PID on a single machine.
• Use the socket module to create and use socket objects.
• Python includes many other higher-level networking modules.
26
27

Weitere ähnliche Inhalte

Was ist angesagt?

Socket programming
Socket programmingSocket programming
Socket programmingharsh_bca06
 
Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sksureshkarthick37
 
How do I construct a python web server using this code? #import socket modul...
How do I construct a python web server using this code?  #import socket modul...How do I construct a python web server using this code?  #import socket modul...
How do I construct a python web server using this code? #import socket modul...hwbloom25
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using javaUC San Diego
 
Opentalk at Large - StS 2005
Opentalk at Large - StS 2005Opentalk at Large - StS 2005
Opentalk at Large - StS 2005Martin Kobetic
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Socketselliando dias
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Python Network Programming – Course Applications Guide
Python Network Programming – Course Applications GuidePython Network Programming – Course Applications Guide
Python Network Programming – Course Applications GuideMihai Catalin Teodosiu
 

Was ist angesagt? (20)

Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Lecture10
Lecture10Lecture10
Lecture10
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
socket programming
socket programming socket programming
socket programming
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
How do I construct a python web server using this code? #import socket modul...
How do I construct a python web server using this code?  #import socket modul...How do I construct a python web server using this code?  #import socket modul...
How do I construct a python web server using this code? #import socket modul...
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Socket programming
Socket programming Socket programming
Socket programming
 
Opentalk at Large - StS 2005
Opentalk at Large - StS 2005Opentalk at Large - StS 2005
Opentalk at Large - StS 2005
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Python Network Programming – Course Applications Guide
Python Network Programming – Course Applications GuidePython Network Programming – Course Applications Guide
Python Network Programming – Course Applications Guide
 

Ähnlich wie Network programming using python

INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptsenthilnathans25
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.pptEloOgardo
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unixswtjerin4u
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23Techvilla
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAMaulik Borsaniya
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsDigitalOcean
 

Ähnlich wie Network programming using python (20)

sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
lab04.pdf
lab04.pdflab04.pdf
lab04.pdf
 
Sockets
Sockets Sockets
Sockets
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 
Sockets
Sockets Sockets
Sockets
 
123
123123
123
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Os 2
Os 2Os 2
Os 2
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking Needs
 
03 sockets
03 sockets03 sockets
03 sockets
 

Mehr von Ali Nezhad

Internet of things
Internet of thingsInternet of things
Internet of thingsAli Nezhad
 
converged Networks
converged Networksconverged Networks
converged NetworksAli Nezhad
 
cnet311 q-bank
cnet311 q-bankcnet311 q-bank
cnet311 q-bankAli Nezhad
 
IP Addressing and subnetting
IP Addressing and subnettingIP Addressing and subnetting
IP Addressing and subnettingAli Nezhad
 
Setting up a WiFi Network v3
Setting up a WiFi Network v3Setting up a WiFi Network v3
Setting up a WiFi Network v3Ali Nezhad
 
adhoc network workshop
adhoc network workshopadhoc network workshop
adhoc network workshopAli Nezhad
 

Mehr von Ali Nezhad (6)

Internet of things
Internet of thingsInternet of things
Internet of things
 
converged Networks
converged Networksconverged Networks
converged Networks
 
cnet311 q-bank
cnet311 q-bankcnet311 q-bank
cnet311 q-bank
 
IP Addressing and subnetting
IP Addressing and subnettingIP Addressing and subnetting
IP Addressing and subnetting
 
Setting up a WiFi Network v3
Setting up a WiFi Network v3Setting up a WiFi Network v3
Setting up a WiFi Network v3
 
adhoc network workshop
adhoc network workshopadhoc network workshop
adhoc network workshop
 

Kürzlich hochgeladen

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 

Kürzlich hochgeladen (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 

Network programming using python

  • 2. OUTLINE • Data Communications Fundamentals • Sockets • Python support for sockets • TCP and UDP communication examples • Higher level programming in Python 2
  • 5. SSH SMTP FTP TRANSPORT LAYER • Provides service to application layer. • Multiplexes applications. • TCP does much more. • Most applications use TCP. • UDP for real-time or tolerant applications. TCP 5 Source: Data and Computer Communications (Stallings)
  • 6. TCP OPERATION 6 Source: Data and Computer Communications (Stallings)
  • 7. TCP VS. UDP 7 Source: Data and Computer Communications (Stallings)
  • 8. IP PACKET 8 Source: Data and Computer Communications (Stallings)
  • 9. SOCKETS • Endpoints of a bidirectional communication channel • Used to address an application on a machine. • Allow multiplexing of applications. • Identified by combination: IP_address + protocol + port_no. • Servers use the well-known port numbers (0 - 1023). • Clients use the ephemeral port numbers (1024 - 65535). • Can be treated as files. 9
  • 10. SOCKET MODULE IN PYTHON • Used in low-level network programming • Usually used by higher level APIs. • Functions correspond to UNIX system calls. • Except makefile() • Examples: socket(), bind(), listen(), accept(), connect(), send(), recv(), close() • Attributes • family, type, proto • Used to create socket objects. • s = socket.socket( family, type, proto) 10 A subtype, usually 0.
  • 11. SOCKET ATTRIBUTES ADDRESS FAMILY • Identifies the domain and a family of protocols • Options for the address family of the socket: • AF_UNIX: a single string for UNIX • Represented by socket.AF_UNIX constant • Addres is just a filename. • AF_INET: a pair of (host, port) for IPv4 • Represented by socket.AF_INET constant • (‘www.yahoo.com’, 80) • (‘209.191.88.254’, 80) • AF_INET6: quadtuple of (host, port, flowid, scopeid) for IPv6 • Represented by socket.AF_INET6 constant 11
  • 12. SOCKET ATTRIBUTES TYPE • Commonly used types: • socket.SOCK_STREAM (for TCP sockets, connection-oriented) • socket.SOCK_DGRAM (for UDP sockets, connectionless) 12
  • 13. SERVER TCP 13 import socket HOST = ‘ ' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() Source: docs.python.org Would be 80 for a web server.
  • 14. SERVER (CONTINUED) TCP 14 import socket HOST = ‘ ' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() Source: docs.python.org
  • 15. SERVER (CONTINUED) TCP 15 import socket HOST = ‘ ' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() Source: docs.python.org
  • 16. SERVER (CONTINUED) TCP 16 import socket HOST = ‘ ' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() Source: docs.python.org
  • 17. SERVER (CONTINUED) TCP 17 import socket HOST = ‘ ' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() Source: docs.python.org
  • 18. SERVER (CONTINUED) TCP 18 import socket HOST = ‘ ' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() Source: docs.python.org
  • 19. CLIENT TCP import socket HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall('Hello, world') data = s.recv(1024) s.close() print 'Received', repr(data) 19 Source: docs.python.org No need to specify local socket.
  • 20. CLIENT (CONTINUED) TCP import socket HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall('Hello, world') data = s.recv(1024) s.close() print 'Received', repr(data) 20 Source: docs.python.org Could be an HTTP request like (‘GET /index.html HTTP/1.0 nn ’). Transport layer gives service to application layer.
  • 23. HIGH LEVEL NETWORK PROGRAMMING • Support for specific applications e.g. HTTP, FTP, … • Instead of socket module, servers use other modules and libraries, e.g.: • HTTP: httplib, urllib2, xmllib • FTP: ftplib, urllib • SMTP: smtplib • Servers usually use the SocketServer module. • Different file formats and encodings must be handled at the client using various libraries such as csv, htmlparser and json. 23
  • 24. SOCKETSERVER EXAMPLE TIME SERVER 24 import SocketServer import time Class TimeHandler(SocketServer.BaseRequestHandler): def handle(self): self.request.sendall(time.ctime()+"n") serv = SocketServer.TCPServer(("",8000),TimeHandler) serv.serve_forever() Source: www.dabeas.com
  • 25. APPLICATION LAYER HTTP EXAMPLE • Opening a web page: 25 import urllib2 u = urllib2.urlopen("http://www.python/org/index.html") data = u.read() print data
  • 26. SUMMARY • Client/server communication model • TCP and UDP give service to applications. • Use TCP sockets if you need reliability. • A socket identifies a single PID on a single machine. • Use the socket module to create and use socket objects. • Python includes many other higher-level networking modules. 26
  • 27. 27