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?
The SocketServer module is a framework for creating network servers. It defines classes for handling synchronous network requests (the server request handler blocks until the request is completed) over TCP, UDP, Unix streams, and Unix datagrams.
serve_forever (poll_interval=0.5) Handle requests until an explicit shutdown() request. Poll for shutdown every poll_interval seconds. Ignores the timeout attribute.
Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.
Python Socket Server To use python socket connection, we need to import socket module. Then, sequentially we need to perform some task to establish connection between server and client. We can obtain host address by using socket. gethostname() function.
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 calls close_request
. In the TCPServer class, close_request
calls self.request.close()
, and self.request
is just the socket used to handle the request.
So the answer to my question is "Yes".
the follow code doesn't work while peer closing
def handle(self):
while 1:
try:
data = self.request.recv(1024)
print self.client_address, "sent", data
except:
pass
this may be better:
def handle(self):
while(1):
try:
self.data = self.request.recv(1024)
if not self.data:
print "%s close" % self.client_address[0]
break
print "%s wrote:" % self.client_address[0], self.data
except:
print "except"
break
reuseaddr
doesn't work as it's too late after bindingserver.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
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:
server_close
somewhere.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 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:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With