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)
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
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