Does the TCPServer + BaseRequestHandler in Python's SocketServer close the socket after each call to handle()?
Question: I'm writing a client/server application in Python and I'm finding it necessary
to get a new connection to the server for each request from the client. My
server is just inheriting from TCPServer and I'm inheriting from
BaseRequestHandler to do my processing. I'm not calling self.request.close()
anywhere in the handler, but somehow the server seems to be hanging up on my
client. What's up?


Answer: _According to the docs_ , neither TCPServer nor BaseRequestHandler close the
socket unless prompted to do so. The default implementations of both
`handle()` and `finish()` do nothing.

A couple of things might be happening:

  * You are closing the socket itself or the request which wraps it, or calling `server_close` somewhere.
  * The socket timeout could have been hit and you have implemented a timeout handler which closes the socket.
  * The client could be actually closing the socket. Code would really help figuring this out.

_However_ , my testing confirms your results. Once you return from `handle` on
the server, whether connecting through telnet or Python's `socket` module,
your connection shows up as being closed by the remote host. Handling socket
activity in a loop inside handle seems to work:

    
    
    def handle(self):
        while 1:
            try:
                data = self.request.recv(1024)
                print self.client_address, "sent", data
            except:
                pass
    

A brief Google Code Search confirms that this is a typical way of handling a
request:
[1](http://www.google.com/codesearch/p?hl=en#kidGrKEZwi0/trunk/bin/stats.py&q=lang%3Apy%20%28BaseRequestHandler%29&sa=N&cd=52&ct=rc)
[2](http://www.google.com/codesearch/p?hl=en#EKZaOgYQHwo/unstable/sources/linda-0.6.tar.gz%7Cg33OoHGt0is/linda-0.6/server.py&q=lang%3Apy%20%28BaseRequestHandler%29&l=118)
[3](http://www.google.com/codesearch/p?hl=en#93sG5NkuQsA/SoHo/1826/srpsocket.zip%7CH78xpN-0ydY/SRPSocket/SRPSocket.py&q=lang%3Apy%20%28BaseRequestHandler%29&l=77)
[4](http://www.google.com/codesearch/p?hl=en#S67yNHIE_f4/authors/id/P/PE/PERRAD/CORBA-
Python-0.36.tar.gz%7C1DH3SMWaXOo/CORBA-
Python-0.36/rpc1/server.py&q=lang%3Apy%20%28BaseRequestHandler%29&l=10).
Honestly, there are plenty of other networking libraries available for Python
that I might look to if I were facing a disconnect between the abstraction
`SocketServer` provides and my expectations.

Code sample that I used to test:

    
    
    from SocketServer import BaseRequestHandler, TCPServer
    
    class TestRequestHandler(BaseRequestHandler):
        def handle(self):
            data = self.request.recv(1024)
            print self.client_address, "sent", data
            self.request.send(data)
    
    class TestServer(TCPServer):
        def __init__(self, server_address, handler_class=TestRequestHandler):
            TCPServer.__init__(self, server_address, handler_class)
    
    if __name__ == "__main__":
        import socket
    
        address = ('localhost', 7734)
        server = TestServer(address)
        server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    

