作为后续不能绑定到地址-after-插座程序崩溃中,我被接收到该误差已重新启动我的程序后:

  

socket.error:[错误98]地址已在使用

在此特定情况下,而不是直接使用插座,该程序被启动它自己的螺纹TCP服务器:

httpd = SocketServer.ThreadingTCPServer(('localhost', port), CustomHandler)
httpd.serve_forever()

如何解决此错误消息?

有帮助吗?

解决方案 2

在该特定情况下,.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)可以从TCPSERVER类当allow_reuse_address选项设置调用。所以我能够解决这个问题,如下所示:

httpd = SocketServer.ThreadingTCPServer(('localhost', port), CustomHandler, False) # Do not automatically bind
httpd.allow_reuse_address = True # Prevent 'cannot bind to address' errors on restart
httpd.server_bind()     # Manually bind, to support allow_reuse_address
httpd.server_activate() # (see above comment)
httpd.serve_forever()

总之,认为这可能是有用的。该解决方案将在Python 3.0略有不同

其他提示

将上述溶液并没有为我工作,但此人做:

   SocketServer.ThreadingTCPServer.allow_reuse_address = True
   server = SocketServer.ThreadingTCPServer(("localhost", port), CustomHandler)
   server.serve_forever()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top