The SocketServer Module

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


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)

server = SocketServer.TCPServer(("", PORT), TimeRequestHandler)
print "listening on port", PORT

connection from ('', 1488)
connection from ('', 1489)

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh © 2008-2020.
If you may any questions please contact us: