[Tutor] Multithreading and SocketServer.TCPServer
Michael P. Reilly
arcege@speakeasy.net
Fri, 20 Jul 2001 08:01:01 -0400 (EDT)
Willi Richert wrote
> uses the TCPServer multithreading? If not, are there equivalent classes =
> that do?
> Examples?
The TCPServer class does not use threading. But the subclass
ThreadingTCPServer does. There is also a ForkingTCPServer and
ThreadingUDPServer. They are all used the same way.
You have a handler class to process a request. Create a subclass
and override the "handle" method. Then pass the subclass to the
Server class.
>>> from SocketServer import ThreadingTCPServer, StreamRequestHandler
>>> class EchoStreamRequestHandler(StreamRequestHandler):
... def handler(self):
... line = self.rfile.readline() # get info from client
... self.wfile.write(line) # write it back to client
...
>>> server_address = ('', 9999) # dummy hostname and socket to listen on
>>> server = ThreadingTCPServer( server_address, EchoStreamRequestHandler )
>>> server.serve_forever() # start up the server
You might want to look at the BaseHTTPServer.py to see how the SocketServer
module is used in other ways.
-Arcege
--
+----------------------------------+-----------------------------------+
| Michael P. Reilly | arcege@speakeasy.net |