[Tutor] Quick networking question
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Apr 18 19:31:58 CEST 2006
On Tue, 18 Apr 2006, Tino Dai wrote:
> I am writing a script to do some simple networking. When I do a
> close on the socket and exit the program, I getting a time wait on the
> port, and the port can''t be utilized again until the time wait
> disappears. How do I get port to shut down and be able to reuse that
> port?
Hi Tino,
Ah! You may want to take a look at some sample use of networking in the
SocketServer:
http://svn.python.org/view/python/trunk/Lib/SocketServer.py?rev=39194&view=markup
In particular, take a look at:
def server_bind(self):
"""Called by constructor to bind the socket.
May be overridden.
"""
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
If we're using SocketServer class to build your socket servers, we set the
allow_reuse_address attribute to True
(See: http://www.python.org/doc/lib/node536.html)
If we're building server sockets from scratch, we'll want to do that magic
line:
socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
right before binding the socket to the address.
Best of wishes!
More information about the Tutor
mailing list