我正在尝试使用线程套接字服务器
self.server = SocketServer.ThreadingTCPServer( ( HOST, PORT ), MCRequestHandler )
和析构函数
def __del__( self ):
self.server.shutdown();
self.server.server_close()
print( 'Server closed ! ' );
当我关闭 GUI 时,将调用 del 函数,但如果我想重新启动该程序,我会收到以下错误消息
socket.error: [Errno 98] Address already in use
Exception AttributeError: "'MCCommunication' object has no attribute 'server'" in <bound method MCCommunication.__del__ of <MCCommunication.MCCommunication object at 0x26867c0>> ignored
最佳答案
创建 TCPServer 的子类,并将其添加到其中:
class TCPServer(SocketServer.TCPServer):
allow_reuse_address = True
与 setsockopt
基本相同,但更简单。
关于Python 套接字服务器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505105/