The SocketServer module provides a framework for various kinds of socket-based servers. The module provides a number of classes that can be mixed and matched to create servers for different purposes.
Example 7-35 implements an Internet Time Protocol server using this module. Use the timeclient script to try it out.
Example 7-35. Using the SocketServer Module
File: socketserver-example-1.py import SocketServer import time # user-accessible port PORT = 8037 # reference time TIME1970 = 2208988800L class TimeRequestHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection from", self.client_address t = int(time.time()) + TIME1970 b = chr(t>>24&255) + chr(t>>16&255) + chr(t>>8&255) + chr(t&255) self.wfile.write(b) server = SocketServer.TCPServer(("", PORT), TimeRequestHandler) print "listening on port", PORT server.serve_forever() connection from ('127.0.0.1', 1488) connection from ('127.0.0.1', 1489) ...
Core Modules
More Standard Modules
Threads and Processes
Data Representation
File Formats
Mail and News Message Processing
Network Protocols
Internationalization
Multimedia Modules
Data Storage
Tools and Utilities
Platform-Specific Modules
Implementation Support Modules
Other Modules