I started a TCP server in Python and tried to send to it multiple requests using a single socket connection:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('127.0.0.1', 4444))
>>> message = b'hi'
>>> s.sendall(message)
>>> resp = s.recv(4096)
>>> resp
b'hello'
>>> # Now trying to send another message without restarting the socket
>>> s.sendall(message)
>>> resp = s.recv(4096)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
The server rejects my second request, I'll have to close and restart the socket so I can send a new request.
This is how my server looks like:
import socketserver
class MyTCPServer(socketserver.BaseRequestHandler):
def handle(self):
message = self.request.recv(4096)
self.request.sendall(b'hello')
server = socketserver.TCPServer(('127.0.0.1', 4444), MyTCPServer)
server.serve_forever()
How can I make the server keep the connection alive and thus accept multiple requests using a single socket?
Here's a sample that shows how the same connected socket can be used for multiple send/receive sequences.
This is not meant to represent robust production-appropriate code:
Output: