简体   繁体   中英

Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM