Socket sample?

Irmen de Jong irmen at -nospam-remove-this-xs4all.nl
Thu May 20 15:31:00 EDT 2004


Daniel Orner wrote:

>     Hmm... I'm looked at the SocketServer, but I'm pretty confused about 
> how to use it. As far as I can tell it doesn't actually do any 
> forking/threading/selecting at all. There's a lot of generic stuff which 
> seems to allow that behavior for subclasses, but I can't see any 
> specific code that does that kind of thing. Or maybe I'm just missing 
> something stupendously obvious. If so, would you be so kind as to give 
> me a quick example of how it would be used?

The SocketServer docs say:
"The solution is to create a separate process or thread to handle each request; the 
ForkingMixIn and ThreadingMixIn mix-in classes can be used to support asynchronous 
behaviour. "

And if you look in SocketServer.py (from python 2.3.3) you'll see at line 479:

class ForkingUDPServer(ForkingMixIn, UDPServer): pass
class ForkingTCPServer(ForkingMixIn, TCPServer): pass

class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass


So it already defines 4 specialized server classes for you to use :-)
Threading, or forking, whatever suits you best.
If you want a TCP server that uses a new thread for each request,
just use SocketServer.ThreadingTCPServer instead of
SocketServer.TCPServer and you're done!

--Irmen



More information about the Python-list mailing list