Sometimes I got this error in the TCP server:
data = connection.recv(4096).decode("utf-8-sig")
File "/usr/lib/python3.6/encodings/utf_8_sig.py", line 23, in decode (output, consumed) = codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 5: invalid continuation byte
This is the code:
server_address = ('xx.xx.xx.xx', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
# Listen for incoming connections. Cantidad de 25 coneciones entrantes en cola
sock.listen(25)
while True:
# Wait for a connection
print ('waiting for a connection')
try:
connection, client_address = sock.accept()
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
#with decode we convert byte to string, default decode is utf-8
data = connection.recv(4096).decode("utf-8-sig")
If I do not put the function .decode("utf-8-sig") I got this error:
TypeError: a bytes-like object is required, not 'str'
How can I prevent this? Previously it used utf-8 and the error rate was higher than utf-8-sig encoding