我有一个在CentOS 6.4/64位下运行的Python协议(protocol)。我的TCP服务器端口为7007。在某些情况下,例如更新新版本或进行维护或在运行时重新启动以刷新缓冲区,我需要以以下方式重新启动应用程序:

server.py:

class AServer(threading.Thread):
  def __init__(self, port):
    threading.Thread.__init__(self)
    self.port = port

  def run(self):
    host = ''
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, self.port))
    print bgcolors.BOOT
    s.listen(1)
    conn, addr = s.accept()
    print bgcolors.OK + 'contact', addr, 'on', self.now()

    while 1:
      try:
        data = conn.recv(1024)
      except socket.error:
        print bgcolors.OK + 'lost', addr, 'waiting..'
        s.listen(1)
        conn, addr = s.accept()
        print bgcolors.OK + 'contact', addr, 'on', self.now()
        continue
      if not data:
        .....
      ...

t = AServer(7007)
t.start()

即时紧急重启(预计在1秒内运行),但失败:
$ ps aux | awk '/server.py/ {print $2}' | head -1 | xargs kill -9;
$ nohup python /var/tmp/py-protocol/server.py &
[root@IPSecVPN protocol]# python server.py
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "server.py", line 236, in run
    s.bind((host, self.port))
  File "<string>", line 1, in bind
error: [Errno 98] Address already in use

最佳答案

您的套接字处于TIME_WAIT状态,这就是即使您的程序已退出,地址仍在使用的原因。您可以在套接字上设置SO_REUSEADDR以在套接字退出TIME_WAIT状态之前重用它。 Python documentation建议以下内容:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))

10-04 15:04