How to generate unique loopback IPv6 address in Python?
<p>I need to be able to generate a unique IPv6 loopback address that I can use to for communication between processes within the host but not outside of it.</p>

<p>For IPv4 I found:</p>

<pre><code>&gt;&gt;&gt; import random, ipaddress
&gt;&gt;&gt; ipaddress.IPv4Address('127.0.0.1') + random.randrange(2**24 - 2)
IPv4Address('127.23.181.175')
</code></pre>

<p>Is there is an analogue for IPv6?</p>

<p>IPv6 only has a single loopback address: <code>::1</code>. This is detailed in <a href="https://tools.ietf.org/html/rfc4291#section-2.5.3" rel="nofollow">RFC 4291, IP Version 6 Addressing Architecture, Section 2.5.3 The Loopback Address</a>:</p>

<blockquote>
  <p>2.5.3.  The Loopback Address</p>
  
  <p>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.</p>
  
  <p>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.</p>
</blockquote>

python ipv6