Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python IPv6 Bytes To Address

I am running python on the iOS App Pythonista 3. When I try to get an IPv6 address it ends up returning bytes rather than a formatted address. Right now I am trying to find a way to either get the address properly without bytes, or to find a way to make the bytes become the address. Here is the code I ran to get the address:

def getIPv6Addr(input):
    return socket.getaddrinfo(input, None, socket.AF_INET6)

and here was the output:

[(30, 2, 17, '', (30, '\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14')), (30, 1, 6, '', (30, '\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14'))]

Edit: The alternative solution is to find what type of encoding is being used to turn this data into bytes.

What Makes:
2607:f8b0:4000:814::200e
become
\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14
like image 348
RoNAPC Avatar asked Jun 08 '26 16:06

RoNAPC


1 Answers

You can use the ipaddress module that is included with python. Just pass the bytes to the constructor of ipaddress.IPv6Address and you'll get an object representing the address and giving you lots of possibilities to print and manipulate it.

like image 200
Sander Steffann Avatar answered Jun 11 '26 21:06

Sander Steffann