Python Socket Getting Connection Reset
Posted
by Ian
on Stack Overflow
See other posts from Stack Overflow
or by Ian
Published on 2010-03-09T18:00:16Z
Indexed on
2010/03/15
0:39 UTC
Read the original article
Hit count: 387
I created a threaded socket listener that stores newly accepted connections in a queue. The socket threads then read from the queue and respond. For some reason, when doing benchmarking with 'ab' (apache benchmark) using a concurrency of 2 or more, I always get a connection reset before it's able to complete the benchmark (this is taking place locally, so there's no external connection issue).
class server:
_ip = ''
_port = 8888
def __init__(self, ip=None, port=None):
if ip is not None:
self._ip = ip
if port is not None:
self._port = port
self.server_listener(self._ip, self._port)
def now(self):
return time.ctime(time.time())
def http_responder(self, conn, addr):
httpobj = http_builder()
httpobj.header('HTTP/1.1 200 OK')
httpobj.header('Content-Type: text/html; charset=UTF-8')
httpobj.header('Connection: close')
httpobj.body("Everything looks good")
data = httpobj.generate()
sent = conn.sendall(data)
def http_thread(self, id):
self.log("THREAD %d: Starting Up..." % id)
while True:
conn, addr = self.q.get()
ip, port = addr
self.log("THREAD %d: responding to request: %s:%s - %s" % (id, ip, port, self.now()))
self.http_responder(conn, addr)
self.q.task_done()
conn.close()
def server_listener(self, host, port):
self.q = Queue.Queue(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind( (host, port) )
sock.listen(5)
for i in xrange(4): #thread count
thread.start_new(self.http_thread, (i+1, ))
while True:
self.q.put(sock.accept())
sock.close()
server('', 9999)
When running the benchmark, I get totally random numbers of good requests before it errors out, usually between 4 and 500.
Edit: Took me a while to figure it out, but the problem was in sock.listen(5)
. Because I was using apache benchmark with a higher concurrency (5 and up) it was causing the backlog of connections to pile up, at which point the connections started getting dropped by the socket.
© Stack Overflow or respective owner