IPv6 address representation in Python

409 views Asked by At

I was converting IPv6 addresses to the textual representation and noticed a behavior I could not explain:

In[38]: socket.inet_ntop(socket.AF_INET6, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x01')
Out[38]: '::ffff:127.0.0.1'
In[39]: socket.inet_ntop(socket.AF_INET6, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00')
Out[39]: '::ff:ffff:7f00:0'

I am surprised to see ::ffff:127.0.0.1, I'd expect it to be ::ffff:7f00:0. Is it standard or at least common? Which IPv6 addresses are represented this way? The Wikipedia article doesn't mention it at all. I am confused.

1

There are 1 answers

0
paxdiablo On BEST ANSWER

The POSIX page for inet_ntop specifies that format as one of the options (slightly paraphrased):

A third form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d, where the x characters are the hexadecimal values of the six high-order 16-bit pieces of the address, and the d characters are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation).