无法将字节转换为字符串

2024-06-26 21:23:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将responseutorrentdht网络从bytes转换为string,但是我得到了UnicodeDecodeError错误

ping_query = {
    't': '0f',
    'y': 'q', 
    'q': 'ping',
    'a': {'id': client_id}}  

ping_query = bencode.encode(ping_query)
host = socket.gethostbyname('router.utorrent.com')
port = 6881

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.connect((host, port))
    s.send(ping_query)
    r = s.recvfrom(1024)

response_data = bencode.decode(r[0])

print(response_data['ip'].decode('utf-8'))

但是我得到了错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 0: invalid continuation byte

response_data['ip']示例:b'\xeb\xff6isQ\xffJ\xec)\xcd\xba\xab\xf2\xfb'

有什么想法吗?你知道吗

编辑:这是来自rb'd2:ip6:\x80j\xe4\x01\xea\xdb1:rd2:id20:\xeb\xff6isQ\xffJ\xec)\xcd\xba\xab\xf2\xfb\xe3F|\xc2ge1:t2:xd1:y1:re'的原始响应


Tags: ipidhostdataportresponse错误socket