我有一个在 CentOS 6.4/64 位下运行的 Python 协议。我在哪里有 TCP 服务器端口 7007。在某些情况下,例如更新新版本或维护或即时重启以刷新缓冲区,我需要重新启动应用程序:
服务器.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