Python套接字间歇性地拒绝连接

时间:2017-04-14 21:37:15

标签: python sockets connection timeout

我有一个接受连接的python套接字(频繁连接/断开连接,每秒多个连接)......

代码:

class ThreadedServer(object):
  def __init__(self, host, port):
    self.host = "0.0.0.0"
    self.port = 9999
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind((self.host, self.port))

  def listen(self):
    self.sock.listen(5)
    while True:
      client, address = self.sock.accept()
      client.settimeout(3)
      threading.Thread(target = self.listenToClient,args = (client,address)).start()

  def listenToClient(self, client, address):
    client.sendall("Welcome\r\n")
    client.close()
    return None

if __name__ == "__main__":
  port_num = 9999
  ThreadedServer('',port_num).listen()

一旦服务器最初启动,响应速度非常快,但是一段时间后,当服务器接受新连接时,我开始获得连接超时....

例如:

root@bugsbunny:/srv# telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
Welcome
Connection closed by foreign host.
root@bugsbunny:/srv# telnet localhost 9999
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection timed out

我检查了一些我能想到的显而易见的事情,它看起来并不是服务器问题......

限制:

root@bugsbunny:/srv# cat /proc/28038/limits 
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             514903               514903               processes 
Max open files            65535                65535                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       514903               514903               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        

Used FDs:
root@bugsgunny:/srv# ls /proc/28038/fd | wc -l
845

任何人都知道这里发生了什么?

日Thnx。

0 个答案:

没有答案