Network Programming With Python
Network Programming With Python
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
communication Reliable (no packet loss or corruption) Preserve packets order (1st sent -> 1st received)
communication Unreliable (no data corruption, but packet loss is possible) Do not preserve packets order
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()
- 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.
more