I’ve tried the below code without success. Basically, I need a way to detect the client disconnection. I’d like to avoid a chatty heartbeat type system. Is there anyway to detect when the socket is no longer open (either client has disconnected or it’s been broken/etc)?
class ThreadedTCPRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
while True:
self.data = self.rfile.readline().strip()
if not self.data:
print 'Client closed connection'
break
I thought this would work, but it doesn’t seem to (also tried with the strip() removed). Anyone have any ideas?
In general, detecting that a client has become ‘silently’ unavailable is not possible. Unless the client closes the connection in a managed fashion, (ie. perfoms the FIN four-way handshake), the server will not detect a dead client unless it tries to communicate with the client and fails.