I'm trying to kick client from the server, and after that I get the follwing exception:
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
During handling of the above exception, another exception occurred:
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.
But when I use the exit function it is going well. Both using the __close_connection
function.
This is my Code:
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()
CodePudding user response:
It seems that you already closed the connection when you did kick and that is why you get the exception. the second exception is when you try to send a message to the closed connection.