我試圖從服務器中踢出客戶端,之后我得到了以下例外:
Exception in thread Thread-2 (__handle_client):
Traceback (most recent call last):
File "/Users/macbook/Desktop/Python Projects/Cars/Server/server.py", line 36, in __handle_client
message = client.recv(1024).decode(FORMAT)
OSError: [Errno 9] Bad file descriptor
在處理上述例外的程序中,又出現了一個例外:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "/Users/macbook/Desktop/Python Projects/Cars/Server/server.py", line 41, in __handle_client
client.send('You left the chat room'.encode(FORMAT))
OSError: [Errno 9] Bad file descriptor.
但是當我使用退出功能時,它進展順利。兩者都使用該__close_connection功能。
這是我的代碼:
def __handle_client(self,client):
while True:
try:
message = client.recv(1024).decode(FORMAT)
if self.__handle_messsage(message,client) == True:
break
except:
client.send('You left the chat room'.encode(FORMAT))
self.__broadcast(f'{self.__clientnick[client]} has left the chat room!',)
del self.__clientnick[client]
client.close()
break
def __handle_messsage(self,message,client):
if message == '/exit':
exit_message = 'You have discinnected successfully.'
self.__close_connction(exit_message,client)
return True
if message.startswith('/kick'):
if self.__clientnick.get(client) == 'admin':
for check_client in self.__clientnick:
if self.__clientnick.get(check_client) == message[6:len(message)]:
kick_message = 'You have been kicket from the chat room.'
self.__close_connction(kick_message,check_client)
return True
else:
client.send('You are not admin!'.encode(FORMAT))
def __close_connection(self,message,client):
client.send(message.encode(FORMAT))
self.__broadcast(f'{self.__clientnick[client]} has left the chat room!',client)
del self.__clientnick[client]
client.close()
uj5u.com熱心網友回復:
似乎您在啟動時已經關閉了連接,這就是您收到例外的原因。第二個例外是當您嘗試向關閉的連接發送訊息時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/416585.html
標籤:
上一篇:非活動期后關閉與客戶端的連接
下一篇:React在事件中重復渲染
