2011-06-28 30 views
4

我試圖使用線程socket服務器Python的SocketServer的問題

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

和析構函數

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

當我關閉GUI的德爾函數將被調用,但是如果我想重新開始程序,我收到以下錯誤消息

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 

回答

3

做TCPSERVER的子類,這給它添加:

class TCPServer(SocketServer.TCPServer): 
allow_reuse_address = True 

基本上一樣setsockopt,但是,更容易。