Rebind TCPserver onthefly ..

Nagy László Zsolt nagylzs at
Thu Mar 20 14:11:45 CET 2003

>I have a xmlrpcserver class, derived from TCPServer. The server does a
>blocking request on a dynamic IP.
>I spawn a thread that compares the address resolved from a dynamic name
>service with that IP bound to the server. If they are different, then the
>dynamic IP has changed and the server should be rebound to the new IP.
>My problem is: How can I restart a TCPServer instance that hangs in a
>blocking handle_request() ?
>Under which conditions will handle_request() abort and return?
>Pinging the server won't work, because he waits on an IP that is not valid
You should not call server_forever. You should create a separate request 
processor thread like this below
(assuming your TCPServer is  named 'server' and you have an Event 
instance named 'server_stopped'):

   srvfd = server.fileno()
   while not server_stopped.isSet():
       ready =[srvfd], [], [], 1) # Give one second for 
incoming connection so we can stop the server in seconds
       if srvfd in ready[0]:
           pass  # No incoming connection, retrying

Now you can start a separate thread and call


to stop request processing when your IP address changes. After your 
request processor stops (use its join() method) you can
rebind the server and restart request processing. I hope this helps.

 Laci 1.0

More information about the Python-list mailing list