3

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
RoNAPC
  • 155
  • 2
  • 9
  • Possibly related? https://stackoverflow.com/questions/1238934/getaddrinfo-in-iphone The 2-tuple you are getting certainly doesn't look like anything that specifies an IPv6 address. – chepner Jun 05 '17 at 01:27
  • I'm not seeing similar results from socket.getaddrinfo in python2 or python3 on Linux. That surprises me a lot. – Sam Hartman Jun 05 '17 at 02:04

3 Answers3

1

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.

Sander Steffann
  • 9,509
  • 35
  • 40
0

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))
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • While attempting running smart_str I found out that pythonista + django doesn't work because django calls subprocesses and iOS blocks subprocessing. Thanks for trying! – RoNAPC Jun 05 '17 at 03:21
0

Solved!

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

>>> getIPv6Addr("google.com")[0][4][1].encode("hex")
'0000000000002607f8b040000811'
RoNAPC
  • 155
  • 2
  • 9