def is_valid_ip(ip):
    # stackoverflow.com/a/4017219/1707152

    def is_valid_ipv4_address(address):
        try:
            socket.inet_pton(socket.AF_INET, address)
        except __HOLE__:
            try:
                socket.inet_aton(address)
            except socket.error:
                return False
            return address.count('.') == 3
        except socket.error:
            return False

        return True

    def is_valid_ipv6_address(address):
        try:
            socket.inet_pton(socket.AF_INET6, address)
        except socket.error:  # not a valid address
            return False
        return True

    return is_valid_ipv4_address(ip) or is_valid_ipv6_address(ip)
dataset/ETHPy150Open opendns/OpenResolve/resolverapi/util/__init__.py/is_valid_ip