Socket woes and signals
Erik Max Francis
max at alcyone.com
Mon Feb 5 12:12:12 EST 2001
Craig Findlay wrote:
> The loop is basically the standard:
>
> while 1:
> conn, addr = s.accept()
> data = conn.recv(bufsize)
> if not data: break
> do something with data
> conn.close()
>
> Q. This is fine if the client closes the connection, but how do I
> break out of the look in response to say a signal?
You can change the signal handler to raise a custom exception which you
can then catch and act on.
> If I force the loop to end ungracefully, the socket is left hanging
> and I have to reboot the machine to free it up.
It takes a few minutes for it to clear, but you won't need to actually
reboot to free it up.
> But the code normally
> stops at the accept line until a connection is accepted, so how do I
> close the socket properly in that state?
If that's all you need, there's an easier way. AFter you create the
socket, call:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Now you won't have to wait to rebind to the address.
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ It is much safer to obey than to rule.
\__/ Thomas a Kempis
7 sisters productions / http://www.7sisters.com/
Web design for the future.
More information about the Python-list
mailing list