Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: 'Address already in use' ... with TCPServer

66 views
Skip to first unread message

Gabriel Genellina

unread,
Jan 30, 2009, 4:16:01 AM1/30/09
to pytho...@python.org
En Fri, 30 Jan 2009 05:43:33 -0200, Mabooka-Mabooka Mbe-Mbe
<ochichiny...@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

Giampaolo Rodola'

unread,
Jan 30, 2009, 7:16:42 AM1/30/09
to
On 30 Gen, 10:16, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
> En Fri, 30 Jan 2009 05:43:33 -0200, Mabooka-Mabooka Mbe-Mbe  
> <ochichinyezaboom...@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(...)

What's even faster is setting the class attribute right after the
module import:

>>> import SocketServer
>>> SocketServer.TCPServer.allow_reuse_address = True


--- Giampaolo
http://code.google.com/p/pyftpdlib

Gabriel Genellina

unread,
Jan 30, 2009, 7:44:22 AM1/30/09
to pytho...@python.org
En Fri, 30 Jan 2009 10:16:42 -0200, Giampaolo Rodola' <gne...@gmail.com>
escribió:

> On 30 Gen, 10:16, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
>> En Fri, 30 Jan 2009 05:43:33 -0200, Mabooka-Mabooka Mbe-Mbe  
>> <ochichinyezaboom...@yahoo.com> escribió:
>>
>> >   setsockopt(REUSEADDR)...
>>
>> >>>> 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(...)
>
> What's even faster is setting the class attribute right after the
> module import:
>
>>>> import SocketServer
>>>> SocketServer.TCPServer.allow_reuse_address = True

...but potentially unsafe if the application uses other servers in other
places. C'mon, defining the new class can be a one-liner also, why take
the risk?

--
Gabriel Genellina

Gabriel Genellina

unread,
Jan 30, 2009, 7:44:22 AM1/30/09
to pytho...@python.org
En Fri, 30 Jan 2009 10:16:42 -0200, Giampaolo Rodola' <gne...@gmail.com>
escribió:
> On 30 Gen, 10:16, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
>> En Fri, 30 Jan 2009 05:43:33 -0200, Mabooka-Mabooka Mbe-Mbe  
>> <ochichinyezaboom...@yahoo.com> escribió:
>>
>> >   setsockopt(REUSEADDR)...
>>
>> >>>> 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(...)
>
> What's even faster is setting the class attribute right after the
> module import:
>
>>>> import SocketServer
>>>> SocketServer.TCPServer.allow_reuse_address = True

...but potentially unsafe if the application uses other servers in other

Gabriel Genellina

unread,
Jan 30, 2009, 8:20:36 AM1/30/09
to pytho...@python.org
En Fri, 30 Jan 2009 10:44:22 -0200, Gabriel Genellina
<gags...@yahoo.com.ar> escribió:

Sorry the duplicate post! I've seen that some of my messages come twice.
I'll try to diagnose and fix the problem (if possible...).

--
Gabriel Genellina

0 new messages