Socket woes and signals

Craig Findlay craigf at
Mon Feb 5 06:52:31 CET 2001

I am writing a simple socket server on an OpenBSD machine, and I would
like to program the following functionality:

If I send the server a SIGTERM signal, I want it to break out of the
loop that it is in, and close gracefully. 

The loop is basically the standard:

while 1:
        conn, addr = s.accept() 
        data = conn.recv(bufsize)
        if not data: break
        do something with data

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?

If I force the loop to end ungracefully, the socket is left hanging
and I have to reboot the machine 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?


More information about the Python-list mailing list