Python socketserver 问题

4 投票
1 回答
680 浏览
提问于 2025-04-16 20:26

我正在尝试使用多线程的套接字服务器

self.server = SocketServer.ThreadingTCPServer( ( HOST, PORT ), MCRequestHandler )

还有析构函数

def __del__( self ):
        self.server.shutdown();
        self.server.server_close()
        print( 'Server closed ! ' );

当我关闭图形用户界面时,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

1 个回答

3

创建一个TCPServer的子类,并在里面添加以下内容:

class TCPServer(SocketServer.TCPServer):
allow_reuse_address = True 

基本上和setsockopt的功能是一样的,但更简单。

撰写回答