如何在 Python 中生成唯一的环回 IPv6 地址?
How to generate unique loopback IPv6 address in Python?
我需要能够生成一个唯一的 IPv6 环回地址,我可以使用该地址在主机内部而非主机外部的进程之间进行通信。
对于 IPv4,我发现:
>>> import random, ipaddress
>>> ipaddress.IPv4Address('127.0.0.1') + random.randrange(2**24 - 2)
IPv4Address('127.23.181.175')
是否有 IPv6 的类似物?
IPv6 只有一个环回地址:::1
。这在 RFC 4291, IP Version 6 Addressing Architecture, Section 2.5.3 The Loopback Address:
中有详细说明
2.5.3. The Loopback Address
The unicast address 0:0:0:0:0:0:0:1 is called the loopback address.
It may be used by a node to send an IPv6 packet to itself. It must
not be assigned to any physical interface. It is treated as having
Link-Local scope, and may be thought of as the Link-Local unicast
address of a virtual interface (typically called the "loopback
interface") to an imaginary link that goes nowhere.
The loopback address must not be used as the source address in IPv6
packets that are sent outside of a single node. An IPv6 packet with
a destination address of loopback must never be sent outside of a
single node and must never be forwarded by an IPv6 router. A packet
received on an interface with a destination address of loopback must
be dropped.
我需要能够生成一个唯一的 IPv6 环回地址,我可以使用该地址在主机内部而非主机外部的进程之间进行通信。
对于 IPv4,我发现:
>>> import random, ipaddress
>>> ipaddress.IPv4Address('127.0.0.1') + random.randrange(2**24 - 2)
IPv4Address('127.23.181.175')
是否有 IPv6 的类似物?
IPv6 只有一个环回地址:::1
。这在 RFC 4291, IP Version 6 Addressing Architecture, Section 2.5.3 The Loopback Address:
2.5.3. The Loopback Address
The unicast address 0:0:0:0:0:0:0:1 is called the loopback address. It may be used by a node to send an IPv6 packet to itself. It must not be assigned to any physical interface. It is treated as having Link-Local scope, and may be thought of as the Link-Local unicast address of a virtual interface (typically called the "loopback interface") to an imaginary link that goes nowhere.
The loopback address must not be used as the source address in IPv6 packets that are sent outside of a single node. An IPv6 packet with a destination address of loopback must never be sent outside of a single node and must never be forwarded by an IPv6 router. A packet received on an interface with a destination address of loopback must be dropped.