SocketServer.threadingtcpserver- 프로그램 재시작 후 주소에 바인딩 할 수 없습니다
문제
후속 조치로 신사-주소-소켓 프로그램 크래쉬, 프로그램이 다시 시작된 후이 오류를 받고있었습니다.
Socket.error : [Errno 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()
제휴하지 않습니다 StackOverflow