Reputation: 77626
def handle_client(self, client_socket, address):
size = 1024
while True:
try:
data = client_socket.recv(size)
if 'q^' in data.decode():
print('Received request for exit from: ' + str(address[0]) + ':' + str(address[1]))
break
else:
body = data.decode()
except socket.error:
client_socket.close()
return False
client_socket.sendall('Received request for exit. Deleted from server threads'.encode())
client_socket.sendall('q^'.encode())
client_socket.close()
The error happens on if 'q^' in data.decode():
The data comes from an iOS client
connection.send(content: "SOME_STRING".data(using: .utf8)!, completion: NWConnection.SendCompletion.contentProcessed({ error in
print(error)
}))
There are similar questions on SO, but non of the solutions solved the problem
Upvotes: 0
Views: 404
Reputation: 12224
On your client side you encoded the characters as bytes using utf-8
.
On your server side you decode the bytes to characters without specifying an encoding. Based on the exception message in your question title, your environment tried to use ascii
to decode the bytes. However, the bytes are not valid for ASCII. You want to explicitly specify the encoding:
data.decode("utf-8")
You should also specify the encoding when you encode your message that you send from your python server.
I would also recommend performing the decode()
only once, rather than twice, for each chunk that you receive.
def handle_client(self, client_socket, address):
size = 1024
while True:
try:
raw_data = client_socket.recv(size)
data = raw_data.decode('utf-8')
if 'q^' in data:
print('Received request for exit from: ' + str(address[0]) + ':' + str(address[1]))
break
else:
body = data
except socket.error:
client_socket.close()
return False
client_socket.sendall('Received request for exit. Deleted from server threads'.encode('utf-8'))
client_socket.sendall('q^'.encode('utf-8'))
client_socket.close()
Upvotes: 1