(af, addr)
Convert an IP address from binary form into text represenation
Convert an IP address from binary form into text represenation
Convert an IP address from binary form into text represenation
def inet_ntop(af, addr):
    """Convert an IP address from binary form into text represenation"""
    if af == socket.AF_INET:
        return inet_ntoa(addr)
    elif af == socket.AF_INET6:
        # IPv6 addresses have 128bits (16 bytes)
        if len(addr) != 16:
            raise Exception("Illegal syntax for IP address")
        parts = []
        for left in [0, 2, 4, 6, 8, 10, 12, 14]:
            try: 
                value = struct.unpack("!H", addr[left:left+2])[0]
                hexstr = hex(value)[2:]
            except TypeError:
                raise Exception("Illegal syntax for IP address")
            parts.append(hexstr.lstrip("0").lower())
        result = ":".join(parts)
        while ":::" in result:
            result = result.replace(":::", "::")
        # Leaving out leading and trailing zeros is only allowed with ::
        if result.endswith(":") and not result.endswith("::"):
            result = result + "0"
        if result.startswith(":") and not result.startswith("::"):
            result = "0" + result
        return result
    else:
        raise Exception("Address family not supported yet")
def inet_ntop ( af , addr ) : if af == socket . AF_INET : return inet_ntoa ( addr ) elif af == socket . AF_INET6 : # IPv6 addresses have 128bits (16 bytes) if len ( addr ) != 16 : raise Exception ( "Illegal syntax for IP address" ) parts = [ ] for left in [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 ] : try : value = struct . unpack ( "!H" , addr [ left : left + 2 ] ) [ 0 ] hexstr = hex ( value ) [ 2 : ] except TypeError : raise Exception ( "Illegal syntax for IP address" ) parts . append ( hexstr . lstrip ( "0" ) . lower ( ) ) result = ":" . join ( parts ) while ":::" in result : result = result . replace ( ":::" , "::" ) # Leaving out leading and trailing zeros is only allowed with :: if result . endswith ( ":" ) and not result . endswith ( "::" ) : result = result + "0" if result . startswith ( ":" ) and not result . startswith ( "::" ) : result = "0" + result return result else : raise Exception ( "Address family not supported yet" )