[Pythonmac-SIG] SO_REUSEADDR not work

Samuel M. Smith smithsm at samuelsmith.org
Fri Feb 13 11:37:22 EST 2004


According to my understanding of the socket library a port can be made 
reusable or used by another socket
with the socket option SO_REUSEADDR however I can't seem to get it to 
work.

here is my test code in file practice.py

def TestReuse(host = '', port = 46000):

    #socket reusable error
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #make socket address reusable
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    ha = (host,port) #bind to Host Address Port
    s.bind(ha)

    return s

Here is a test

 >>> import practice
 >>> s = practice.TestReuse()
 >>> s
<socket._socketobject object at 0x53ba0>
 >>> t = practice.TestReuse()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/Users/smithsm/Data/Adept/Products/code/pylon/practice.py", 
line 370, in TestReuse
     s.bind(ha)
   File "<string>", line 1, in bind
socket.error: (48, 'Address already in use')
 >>> s.close()
 >>> t = practice.TestReuse()
 >>> t
<socket._socketobject object at 0x420120>
 >>>

I get a reuse error.
If I exit out of python and then restart I do not get the reuse error.

If I open another shell and run another copy of python I also get the 
reuse error.

Is this a bug or do I just misunderstand how it is supposed to work.

What does exiting python do to opened sockets.







More information about the Pythonmac-SIG mailing list