在这种特定情况下,
.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)可以在
allow_reuse_address设置选项时从TCPServer类调用。因此,我能够按以下方式解决它:
httpd = SocketServer.ThreadingTCPServer(('localhost', port), CustomHandler, False) # Do not automatically bindhttpd.allow_reuse_address = True # Prevent 'cannot bind to address' errors on restarthttpd.server_bind() # Manually bind, to support allow_reuse_addresshttpd.server_activate() # (see above comment)httpd.serve_forever()无论如何,以为这可能有用。该解决方案在Python 3.0中将略有不同



