def InetNtoA(packed_bytes):

  if len(packed_bytes) != 16:
    raise socket.error("IPv6 addresses are 16 bytes long, got %s for %s" %
                       (len(packed_bytes), packed_bytes))

  hex_encoded = packed_bytes.encode("hex_codec")

  # Detect IPv4 endings
  if hex_encoded.startswith("00000000000000000000ffff"):
    return "::ffff:" + socket.inet_ntoa(packed_bytes[-4:])

  # Detect IPv4 endings. If the first quad is 0, it isn't IPv4.
  if hex_encoded.startswith("0" * 24) and not hex_encoded.startswith("0" * 28):
    return "::" + socket.inet_ntoa(packed_bytes[-4:])

  # Split into quads
  chunked = [hex_encoded[i:i + 4] for i in xrange(0, len(hex_encoded), 4)]

  output = []
  for chunk in chunked:
    # Strip leading zeros
    chunk = "".join(chunk).lstrip("0")
    if not chunk:
      # Set all 0 chunks to a single 0
      chunk = "0"
    output.append(chunk)

  result_str = ":".join(output)

  # Compress with :: by finding longest sequence of zeros that look like :0:0:0
  # or 0:0:0 if its the start of the string
  matches = ZERO_SEQUENCE.findall(result_str)
  if matches:
    largest_zero_str = max(matches, key=len)

    if len(largest_zero_str) > 3:
      # Replace any zero string longer than :0: with ::
      result_str = result_str.replace(largest_zero_str, "::", 1)

  return result_str
Convert ipv6 packed bytes to string.
dataset/ETHPy150Open google/grr/grr/lib/ipv6_utils.py/InetNtoA