gpt4 book ai didi

python TCPServer 地址已在使用中,但我关闭服务器并使用 `allow_reuse_address`

转载 作者:太空狗 更新时间:2023-10-29 17:55:32 24 4
gpt4 key购买 nike

这是我运行服务器的代码:

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。

最佳答案

感谢其他答案,我想通了。 allow_reuse_address 应该在类上,而不是在实例上:

SocketServer.TCPServer.allow_reuse_address = True
httpd = SocketServer.TCPServer(("", PORT), MyRequestHandler)

不过,我仍然不确定为什么关闭套接字没有为服务器的下一次运行释放它。

关于python TCPServer 地址已在使用中,但我关闭服务器并使用 `allow_reuse_address`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com