Network Programming with Python
The Communication Model
The Python basic network communication API is the socket module. Its API is similar to the common Unix\Windows C API. The communication is between sockets, that are file descriptors to a remote host. A socket is built on:
IP
address Port
Socket Types
Stream sockets (SOCK_STREAM) \ TCP
Connection-Oriented
communication Reliable (no packet loss or corruption) Preserve packets order (1st sent -> 1st received)
Datagram sockets (SOCK_DGRAM) \ UDP
Connectionless
communication Unreliable (no data corruption, but packet loss is possible) Do not preserve packets order
The Client-Server Model Connection-Oriented Flow Diagram
The Client-Server Model Connectionless Flow Diagram
A Simple UDP Client
import socket server_host, server_port = ('127.0.0.1', 50007) server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) client_str = "Hello, UDP-Server!" server_sock.sendto(client_str, (server_host, server_port) ) # The client can also use server_sock.recvfrom(). server_sock.close()
A Simple UDP Server
import socket server_host, server_port = (, 50007) # listen on all interfaces, on port 50007 client_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) client_sock.bind( (server_host, server_port) ) # while True: client_str, client_addr = client_sock.recvfrom(65535) print 'Server Received: ', client_str # The server can also use client_sock.sendto(). client_sock.close() # Only once.
A Simple UDP Server Using SocketServer import SocketServer
class myUDPServer(SocketServer.DatagramRequestHandler): def handle (self): #Receive data: client_str = self.rfile.read() print 'Server Received: ', client_str # The server can also use self.wfile.write(). server_host, server_port = ('', 50007) # listen on all interfaces, on port 50007. server = SocketServer.UDPServer((server_host, server_port), myUDPServer) server.handle_request() # or server.serve_forever()
A Simple TCP Client
import socket, struct server_host, server_port = ('127.0.0.1', 50007) server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.connect( (server_host, server_port) ) client_str = "Hello, TCP-Server!" server_sock.sendall(struct.pack('L', len(client_str))) server_sock.sendall(client_str) # The client can also use server_sock.recv(). server_sock.close()
A Simple TCP Server
import socket, struct server_host, server_port = (, 50007) # listen on all interfaces, on port 50007 listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_sock.bind( (server_host, server_port) ) listen_sock.listen(3) # while True: client_sock, client_addr = listen_sock.accept() client_data = client_sock.recv(struct.calcsize('L')) client_str_len, = struct.unpack('L', client_data) client_str = client_sock.recv(client_str_len) print 'Server Received: ', client_str # The server can also use client_sock.send(). listen_sock.close() # Only once.
import SocketServer, struct
A Simple TCP Server Using SocketServer
class myTCPServer(SocketServer.StreamRequestHandler): def handle (self): #Receive data: client_data = self.rfile.read(struct.calcsize('L')) client_str_len, = struct.unpack('L', client_data) client_str = self.rfile.read(client_str_len) print 'Server Received: ', client_str # The server can also use self.wfile.write(). server_host, server_port = ('', 50007) # listen on all interfaces, on port 50007. server = SocketServer.TCPServer((server_host, server_port), myTCPServer) server.handle_request() # or server.serve_forever()
Other Relevant Modules
Parts of the standard library:
cgi
- Used to interpret forms in server-side scripts. httplib - HTTP and HTTPS protocol client. ftplib FTP protocol client. smtplib SMTP protocol client. telnetlib - Telnet client.
External networking frameworks in Python:
Twisted TurboGears Django RPyC Many
more