0

I am trying to convert the response from utorrent dht network from bytes to string but I get UnicodeDecodeError errors

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

However i get the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 0: invalid continuation byte

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

Any thoughts?

Edit: This is the raw response from r: b'd2:ip6:\x80j\xe4\x01\xea\xdb1:rd2:id20:\xeb\xff6isQ\xffJ\xec)\xcd\xba\xab\xf2\xfb\xe3F|\xc2ge1:t2:xd1:y1:re'

jake wong
  • 4,909
  • 12
  • 42
  • 85

0 Answers0