python TCPServer地址已在使用中,但我关闭了服务器,并使用了"allow_reuse_address" [英] python TCPServer address already in use but I close the server and I use `allow_reuse_address`
本文介绍了python TCPServer地址已在使用中,但我关闭了服务器,并使用了"allow_reuse_address"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我运行服务器的代码:
Here is my code to run the server:
class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
#....
PORT = 8089
httpd = SocketServer.TCPServer(("", PORT), MyRequestHandler)
httpd.allow_reuse_address = True
print "Serving forever at port", PORT
try:
httpd.serve_forever()
except:
print "Closing the server."
httpd.server_close()
raise
这是发生了什么
^CClosing the server.
Traceback (most recent call last):
File "server.py", line 118, in <module>
self.send_error(400, "Unimplemented GET command: %s" % (self.path,))
File "/home/claudiu/local/lib/python2.6/SocketServer.py", line 224, in serve_forever
r, w, e = select.select([self], [], [], poll_interval)
KeyboardInterrupt
(.virtualenv)claudiu@xxx:~/xxx$ python server.py
Traceback (most recent call last):
File "server.py", line 122, in <module>
httpd = SocketServer.TCPServer(("", PORT), MyRequestHandler)
File "/home/claudiu/local/lib/python2.6/SocketServer.py", line 402, in __init__
self.server_bind()
File "/home/claudiu/local/lib/python2.6/SocketServer.py", line 413, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use
为什么?我关闭服务器并将allow_reuse_address
设置为True ...使用python 2.6.8.
Why? I close the server and set allow_reuse_address
to True... Using python 2.6.8.
推荐答案
感谢其他答案,我发现了. allow_reuse_address
应该在类上,而不在实例上:
Thanks to the other answers, I figured it out. allow_reuse_address
should be on the class, not on the instance:
SocketServer.TCPServer.allow_reuse_address = True
httpd = SocketServer.TCPServer(("", PORT), MyRequestHandler)
我仍然不确定为什么关闭套接字并不能将其释放给服务器的下一次运行.
I'm still not sure why closing the socket didn't free it up for the next run of the server, though.
这篇关于python TCPServer地址已在使用中,但我关闭了服务器,并使用了"allow_reuse_address"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文