[Tutor] More network server questions

Kent Johnson kent37 at tds.net
Tue Jun 20 17:39:45 CEST 2006

Tino Dai wrote:
>     so, I think you've to set the allow_reuse_address for the TCPServer and
>     not for the RequestHandler
> Hi there Ewald,
>      I tried that, and still no joy. Now I have:
> class Listener( threading.Thread ):
>         sema = threading.Semaphore()
>         def __init__(self):
>                 self.port=xxxx
>                 self.ipAddr='xxx.xxx.xxx.xxx'
>                 self.wholeFilenameList=[]
>                 threading.Thread.__init__(self)
>                 #print "Done with init"
>         def run(self):
>                 server=SocketServer.TCPServer(('',self.port), FtpServer)
>                 server.allow_reuse_address = 1    <- I moved the 
> server.allow_reuse_adddress from the FtpServer, which was 
> SocketServer.StreamRequestHandler
>                 print "The port is: ", self.port
>                 server.serve_forever()
> Any clue on what I am missing?

Looking at the source code for SocketServer, you have to set 
allow_reuse_address *before* instantiating the server, because the value 
is used by the __init__() method. allow_reuse_address is actuall a class 
attribute, not an instance attribute.

One way to do this is to set the class attribute directly:
SocketServer.TCPServer.allow_reuse_address = 1

This will change the behaviour of any TCPServer created after this 
assignment. If you want to just change the one server you create, make 
your own server subclass and set allow_reuse_address there:

class MyServer(SocketServer.TCPServer):
     allow_reuse_address = True

then instantiate MyServer instead of TCPServer.


More information about the Tutor mailing list