How to use the maxminddb.compat.compat_ip_address function in maxminddb

To help you get started, we’ve selected a few maxminddb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github maxmind / MaxMind-DB-Reader-python / maxminddb / reader.py View on Github external
def get_with_prefix_len(self, ip_address):
        """Return a tuple with the record and the associated prefix length


        Arguments:
        ip_address -- an IP address in the standard string notation
        """
        if isinstance(ip_address, string_type):
            address = compat_ip_address(ip_address)
        else:
            address = ip_address

        try:
            packed_address = bytearray(address.packed)
        except AttributeError:
            raise TypeError('argument 1 must be a string or ipaddress object')

        if address.version == 6 and self._ip_version == 4:
            raise ValueError(
                'Error looking up {0}. You attempted to look up '
                'an IPv6 address in an IPv4-only database.'.format(ip_address))

        (pointer, prefix_len) = self._find_address_in_tree(packed_address)

        if pointer:
github m-lab / mlab-ns / server / mlabns / third_party / geoip2 / maxminddb / reader.py View on Github external
def get(self, ip_address):
        """Return the record for the ip_address in the MaxMind DB


        Arguments:
        ip_address -- an IP address in the standard string notation
        """
        if not isinstance(ip_address, string_type):
            raise TypeError('argument 1 must be %s, not %s' %
                            (string_type_name, type(ip_address).__name__))

        address = compat_ip_address(ip_address)

        if address.version == 6 and self._metadata.ip_version == 4:
            raise ValueError(
                'Error looking up {0}. You attempted to look up '
                'an IPv6 address in an IPv4-only database.'.format(ip_address))
        pointer = self._find_address_in_tree(address)

        return self._resolve_data_pointer(pointer) if pointer else None