2011-06-28 15 views
4

私はスレッドソケットサーバ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ののサブクラスを作成し、それにこれを追加します。setsockoptとして

class TCPServer(SocketServer.TCPServer): 
allow_reuse_address = True 

基本的には同じですが、より簡単。