Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>According to the docs</em>, neither TCPServer nor BaseRequestHandler close the socket unless prompted to do so. The default implementations of both <code>handle()</code> and <code>finish()</code> do nothing. </p> <p>A couple of things might be happening:</p> <ul> <li>You are closing the socket itself or the request which wraps it, or calling <code>server_close</code> somewhere.</li> <li>The socket timeout could have been hit and you have implemented a timeout handler which closes the socket.</li> <li>The client could be actually closing the socket. Code would really help figuring this out.</li> </ul> <p><em>However</em>, my testing confirms your results. Once you return from <code>handle</code> on the server, whether connecting through telnet or Python's <code>socket</code> module, your connection shows up as being closed by the remote host. Handling socket activity in a loop inside handle seems to work:</p> <pre><code>def handle(self): while 1: try: data = self.request.recv(1024) print self.client_address, "sent", data except: pass </code></pre> <p>A brief Google Code Search confirms that this is a typical way of handling a request: <a href="http://www.google.com/codesearch/p?hl=en#kidGrKEZwi0/trunk/bin/stats.py&amp;q=lang:py%20(BaseRequestHandler)&amp;sa=N&amp;cd=52&amp;ct=rc" rel="noreferrer">1</a> <a href="http://www.google.com/codesearch/p?hl=en#EKZaOgYQHwo/unstable/sources/linda-0.6.tar.gz%7Cg33OoHGt0is/linda-0.6/server.py&amp;q=lang:py%20(BaseRequestHandler)&amp;l=118" rel="noreferrer">2</a> <a href="http://www.google.com/codesearch/p?hl=en#93sG5NkuQsA/SoHo/1826/srpsocket.zip%7CH78xpN-0ydY/SRPSocket/SRPSocket.py&amp;q=lang:py%20(BaseRequestHandler)&amp;l=77" rel="noreferrer">3</a> <a href="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&amp;q=lang:py%20(BaseRequestHandler)&amp;l=10" rel="noreferrer">4</a>. 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 <code>SocketServer</code> provides and my expectations.</p> <p>Code sample that I used to test:</p> <pre><code>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) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload