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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- Multiple sockets for clients to connect to
- How to get the background from multiple images by
- Evil ctypes hack in python
According to the docs, neither TCPServer nor BaseRequestHandler close the socket unless prompted to do so. The default implementations of both
handle()
andfinish()
do nothing.A couple of things might be happening:
server_close
somewhere.However, my testing confirms your results. Once you return from
handle
on the server, whether connecting through telnet or Python'ssocket
module, your connection shows up as being closed by the remote host. Handling socket activity in a loop inside handle seems to work:A brief Google Code Search confirms that this is a typical way of handling a request: 1 2 3 4. 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:
the follow code doesn't work while peer closing
this may be better:
reuseaddr
doesn't work as it's too late after bindingYou sure the client is not hanging up on the server? This is a bit too vague to really tell what is up, but generally a server that is accepting data from a client will quit the connection of the read returns no data.
Okay, I read the code (on my Mac, SocketServer.py is at /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/).
Indeed, TCPServer is closing the connection. In
BaseServer.handle_request
,process_request
is called, which callsclose_request
. In the TCPServer class,close_request
callsself.request.close()
, andself.request
is just the socket used to handle the request.So the answer to my question is "Yes".