简体   繁体   中英

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

there is a quick way if you use this package:

from django.utils.encoding import smart_str

a = '\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14'


print(smart_str(a))

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.

Solved!

I encoded the bytes with hex and it made it become the address!

>>> getIPv6Addr("google.com")[0][4][1].encode("hex")
'0000000000002607f8b040000811'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM