problem with SocketServer -- address already in use

Charles G Waldman cgw at fnal.gov
Tue Jul 13 18:29:31 EDT 1999


Phil Hunt writes:
 > I'm trying to use SocketServer.py to write a TCP server. I ran
 > my program with ``python serv.py'', which went into an infinite
 > loop (which is what it should do). I exited my program with ^Z,
 > and tried to start it up again. I got an error message:
 > 
 > socket.error: (98, 'Address already in use')
 > 
 > which I presume is because something thinks the old invokation of
 > my program is still using port 7070. 

Did you actually kill the process after doing ^Z?  ^Z just suspends a process.

 > How do I clear the port, before I start up my server?

You don't.  The kernel clears the port, after a certain period of time 
has elapsed.  However, if the old process really is gone, and you want
to bind to the same address, without waiting for the kernel, use the 
SO_REUSEADDR flag to setsockopt:

self.socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)

This must be done after a socket is created and before you call listen 
or bind.

I don't see an easy way to do this within the framework of SocketServer.py

It might be nice if there were an optional flag in SocketServer to
allow the user to specify this behaviour.





More information about the Python-list mailing list