Python中的IPv6地址表示

2024-03-29 10:02:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将IPv6地址转换为文本表示,注意到一个我无法解释的行为:

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'

我很惊讶地看到::ffff:127.0.0.1,我希望它是{}。它是标准的还是至少普通的?哪些IPv6地址是这样表示的?Wikipedia article根本没有提到它。我很困惑。在


Tags: in文本地址socketoutipv6afinet
1条回答
网友
1楼 · 发布于 2024-03-29 10:02:06

POSIX page for ^{}将该格式指定为选项之一(稍作解释):

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

相关问题 更多 >