0

MY CODE

import socket

BUFFER_SIZE = 1024
HOST = '192.168.1.3'
PORT = 5555        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(10)
    while 1:
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr[0] + ":" + str(addr[1]))
            msg = conn.recv(BUFFER_SIZE)

            print(msg.decode('UTF-8'))

and when I execute the code I get a print (msg.decode (encoding = 'UTF-8', errors = 'strict')) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 6: invalid start byte you can help me

  • 3
    It seems that you are not receiving valid utf8 strings, maybe the content your trying to decode is gzipped ( [source](https://stackoverflow.com/questions/44659851/unicodedecodeerror-utf-8-codec-cant-decode-byte-0x8b-in-position-1-invalid#44660123)), but its impossible to know without knowing what your expecting or seeing the raw data – Custards1 Jul 04 '21 at 17:21
  • You can try .decode('utf-8', errors='ignore') to see if anything can be decoded, and investigate what part of the text is giving you trouble. Maybe you are not getting utf-8 and you need utf-16 or something else? See https://stackoverflow.com/questions/36897662/trouble-decoding-utf-16-string – Hammurabi Jul 04 '21 at 19:37
  • It's impossible to help you without seeing the actual content of `msg`. The rest of the code is irrelevant. The short answer is whatever you received isn't encoded in UTF-8. – Mark Tolonen Jul 06 '21 at 04:22
  • Please [edit] your question to provide `msg.decode (encoding = 'UTF-8', errors = 'backslashreplace')` in your [mcve]. (Share _sanitized_ data.) – JosefZ Jul 06 '21 at 18:54

0 Answers0