'Address already in use' ... with TCPServer

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Jan 30 04:16:01 EST 2009


En Fri, 30 Jan 2009 05:43:33 -0200, Mabooka-Mabooka Mbe-Mbe  
<ochichinyezaboombwa at yahoo.com> escribió:

>   setsockopt(REUSEADDR)...
>
> What I came up with so far is this:
>>>> from SocketServer import *
>>>> s = TCPServer( ('', 32123), None)
>>>> dir(s)
> ['RequestHandlerClass', '__doc__', '__init__', '__module__',  
> 'address_family', 'allow_reuse_address', ... ]
>
> Aha! My bet is (was):
>>>> s.allow_reuse_address=1
> should do the trick.

It's too late then; bind() has already been called. The easiest way is to  
define your own derived class:

import SocketServer

class TCPServer(SocketServer.TCPServer):
     allow_reuse_address = True

s = TCPServer(...)

> I acknowledge that I am trying to hack it rather then looking at all the  
> web 1st:
>  sorry if I am spamming this list while good documentation exists. But  
> does it?

Sure:

http://docs.python.org/library/socketserver.html#SocketServer.allow_reuse_address

The server classes support the following class variables:
SocketServer.allow_reuse_address
Whether the server will allow the reuse of an address. This defaults to  
False, and can be set in subclasses to change the policy.

Reading the source may help too :)

-- 
Gabriel Genellina




More information about the Python-list mailing list