title: Create ipaddr-py IPv6Address from byte string
body: <p>I frequently need to convert a raw, byte-encoded IPv6 address into an IPv6Address object from the <a href="http://code.google.com/p/ipaddr-py/" rel="nofollow">ipaddr-py project</a>. Byte-encoded IPv6 addresses are not accepted by the initializer as shown here:</p>
  
  <pre><code>&gt;&gt;&gt; import ipaddr   
  &gt;&gt;&gt; byte_ip = b'\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
  &gt;&gt;&gt; ipaddr.IPAddress(byte_ip)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "ipaddr.py", line 78, in IPAddress
      address)
  ValueError: ' \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' does
   not appear to be an IPv4 or IPv6 address
  </code></pre>
  
  <p>What is the easiest way to convert the byte-encoding to a format ipaddr-py can understand?
  I'm using v. 2.1.10 of ipaddr.py.</p>
  
  <p>My only workaround so far is way too long for the simple task:</p>
  
  <pre><code>&gt;&gt;&gt; def bytes_to_ipaddr_string(c):
  ...     c = c.encode('hex')
  ...     if len(c) is not 32: raise Exception('invalid IPv6 address')
  ...     s = ''
  ...     while c is not '':
  ...         s = s + ':'
  ...         s = s + c[:4]
  ...         c = c[4:]
  ...     return s[1:]
  ...
  &gt;&gt;&gt; ipaddr.IPAddress(bytes_to_ipaddr_string(byte_ip))
  IPv6Address('2000::1')
  </code></pre>
  
  <p><strong>EDIT:</strong> I'm looking for a cross-platform solution. Unix-only won't do.</p>
  
  <p>Anyone got a better solution?</p>