Example #1
0
    def record_by_addr(self, addr):
        """
        Look up the record for a given IP address.
        Use this method if you have a City database.

        @param addr: IP address
        @type addr: str
        @return: Dictionary with country_code, country_code3, country_name,
            region, city, postal_code, latitude, longitude, dma_code,
            metro_code, area_code, region_name, time_zone
        @rtype: dict
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError('Invalid IP address')

            if self._databaseType not in const.CITY_EDITIONS:
                message = 'Invalid database type, expected City'
                raise GeoIPError(message)

            rec = self._get_record(ipnum)
            if not rec:
                return None

            return rec
        except ValueError:
            raise GeoIPError('Failed to lookup address %s' % addr)
Example #2
0
    def region_by_addr(self, addr):
        """
        Lookup the region for given IP address.
        Use this method if you have a Region database.

        @param addr: IP address
        @type addr: str
        @return: dict containing country_code, region,
            and region_name
        @rtype: dict
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if not self._databaseType in (const.REGION_EDITION_REV0, const.REGION_EDITION_REV1,
                                          const.CITY_EDITION_REV0, const.CITY_EDITION_REV1):
                raise GeoIPError('Invalid database type; region_* methods expect '\
                                 'Region or City database')

            return self._get_region(ipnum)
        except ValueError:
            raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % addr)
Example #3
0
    def org_by_addr(self, addr):
        """
        Lookup the organization (or ISP) for given IP address.
        Use this method if you have an Organization/ISP database.

        @param addr: IP address
        @type addr: str
        @return: organization or ISP name
        @rtype: str
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if self._databaseType not in (const.ORG_EDITION, const.ISP_EDITION,
                                          const.ASNUM_EDITION):
                raise GeoIPError('Invalid database type; org_* methods expect '\
                                 'Org/ISP database')

            return self._get_org(ipnum)
        except ValueError:
            raise GeoIPError(
                '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)'
                % addr)
Example #4
0
    def region_by_addr(self, addr):
        """
        Lookup the region for given IP address.
        Use this method if you have a Region database.

        @param addr: IP address
        @type addr: str
        @return: dict containing country_code, region,
            and region_name
        @rtype: dict
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if not self._databaseType in (
                    const.REGION_EDITION_REV0, const.REGION_EDITION_REV1,
                    const.CITY_EDITION_REV0, const.CITY_EDITION_REV1):
                raise GeoIPError('Invalid database type; region_* methods expect '\
                                 'Region or City database')

            return self._get_region(ipnum)
        except ValueError:
            raise GeoIPError(
                '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)'
                % addr)
Example #5
0
    def record_by_addr(self, addr):
        """
        Look up the record for a given IP address.
        Use this method if you have a City database.

        @param addr: IP address
        @type addr: str
        @return: dict with country_code, country_code3, country_name,
            region, city, postal_code, latitude, longitude,
            dma_code, metro_code, area_code, region_name, time_zone
        @rtype: dict
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if not self._databaseType in (const.CITY_EDITION_REV0,
                                          const.CITY_EDITION_REV1):
                raise GeoIPError(
                    'Invalid database type; record_* methods expect City database'
                )

            return self._get_record(ipnum)
        except ValueError:
            raise GeoIPError(
                '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)'
                % addr)
Example #6
0
    def record_by_addr(self, addr):
        """
        Look up the record for a given IP address.
        Use this method if you have a City database.

        @param addr: IP address
        @type addr: str
        @return: Dictionary with country_code, country_code3, country_name,
            region, city, postal_code, latitude, longitude, dma_code,
            metro_code, area_code, region_name, time_zone
        @rtype: dict
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError('Invalid IP address')

            if self._databaseType not in const.CITY_EDITIONS:
                message = 'Invalid database type, expected City'
                raise GeoIPError(message)

            rec = self._get_record(ipnum)
            if not rec:
                return None

            return rec
        except ValueError:
            raise GeoIPError('Failed to lookup address %s' % addr)
Example #7
0
    def time_zone_by_addr(self, addr):
        """
        Look up the time zone for a given IP address.
        Use this method if you have a Region or City database.

        @param hostname: IP address
        @type hostname: str
        @return: Time zone
        @rtype: str
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if not self._databaseType in (
                    const.REGION_EDITION_REV0, const.REGION_EDITION_REV1,
                    const.CITY_EDITION_REV0, const.CITY_EDITION_REV1):
                raise GeoIPError('Invalid database type; region_* methods expect '\
                                 'Region or City database')

            return self._get_record(ipnum)['time_zone']
        except ValueError:
            raise GeoIPError(
                '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)'
                % addr)
Example #8
0
    def id_by_addr(self, addr):
        """
        Returns the database ID for specified address.
        The ID might be useful as array index. 0 is unknown.

        :arg addr: IPv4 or IPv6 address (eg. 203.0.113.30)
        """
        if self._databaseType in (const.PROXY_EDITION,
                                  const.NETSPEED_EDITION_REV1,
                                  const.NETSPEED_EDITION_REV1_V6):
            raise GeoIPError(
                'Invalid database type; this database is not supported')
        ipv = 6 if addr.find(':') >= 0 else 4
        if ipv == 4 and self._databaseType not in (const.COUNTRY_EDITION,
                                                   const.NETSPEED_EDITION):
            raise GeoIPError(
                'Invalid database type; this database supports IPv6 addresses, not IPv4'
            )
        if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
            raise GeoIPError(
                'Invalid database type; this database supports IPv4 addresses, not IPv6'
            )

        ipnum = util.ip2long(addr)
        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #9
0
    def lookup(self, addr):
        addrlong = ip2long(addr)

        data = DictObject()
        data.addr = addr

        for location_ip in LocationIP.objects.all():
            if ((addrlong & ip2long(location_ip.mask)) == ip2long(location_ip.address)):
                location = location_ip.location
                data.latitude = location.latitude
                data.longitude = location.longitude
                data.name = location.name
                match = True
                break

        if match:
            return data
        else:
            return None
Example #10
0
    def lookup(self, addr):
        addrlong = ip2long(addr)

        data = DictObject()
        data.addr = addr

        for location_ip in LocationIP.objects.all():
            if ((addrlong & ip2long(location_ip.mask)) == ip2long(
                    location_ip.address)):
                location = location_ip.location
                data.latitude = location.latitude
                data.longitude = location.longitude
                data.name = location.name
                match = True
                break

        if match:
            return data
        else:
            return None
Example #11
0
    def region_by_addr(self, addr):
        """
        Returns dictionary containing `country_code` and `region_code`.

        :arg addr: IP address (e.g. 203.0.113.30)
        """
        if self._databaseType not in const.REGION_CITY_EDITIONS:
            message = 'Invalid database type, expected Region or City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_region(ipnum)
Example #12
0
    def time_zone_by_addr(self, addr):
        """
        Returns time zone in tzdata format (e.g. America/New_York or Europe/Paris)

        :arg addr: IP address (e.g. 203.0.113.30)
        """
        if self._databaseType not in const.CITY_EDITIONS:
            message = 'Invalid database type, expected City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_record(ipnum).get('time_zone')
Example #13
0
    def org_by_addr(self, addr):
        """
        Returns Organization, ISP, or ASNum name for given IP address.

        :arg addr: IP address (e.g. 203.0.113.30)
        """
        valid = (const.ORG_EDITION, const.ISP_EDITION, const.ASNUM_EDITION, const.ASNUM_EDITION_V6)
        if self._databaseType not in valid:
            message = 'Invalid database type, expected Org, ISP or ASNum'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_org(ipnum)
Example #14
0
    def lookup(self, addr):
        data = DictObject()
        data.addr = addr
        data.internal = False

        with lookup_lock:
            r = self.geoip.record_by_addr(addr)

        match = False

        if r is not None:
            data.latitude = r['latitude']
            data.longitude = r['longitude']
            match = True
            try:
                (n, x, y) = socket.gethostbyaddr(addr)
                data.name = n
            except:
                data.name = None

        else:
            addrlong = ip2long(addr)

            for location in Location.objects.all():
                if ((addrlong & ip2long(location.mask)) == ip2long(
                        location.address)):
                    data.latitude = location.latitude
                    data.longitude = location.longitude
                    data.name = location.name
                    data.internal = True
                    match = True
                    break

        if match:
            return data
        else:
            return None
Example #15
0
    def lookup(self, addr):
        data = DictObject()
        data.addr = addr
        data.internal = False

        with lookup_lock:
            r = self.geoip.record_by_addr(addr)

        match = False

        if r is not None:
            data.latitude = r['latitude']
            data.longitude = r['longitude']
            match = True
            try:
                (n, x, y) = socket.gethostbyaddr(addr)
                data.name = n
            except:
                data.name = None

        else:
            addrlong = ip2long(addr)
            
            for location in Location.objects.all():
                if ((addrlong & ip2long(location.mask)) == ip2long(location.address)):
                    data.latitude = location.latitude
                    data.longitude = location.longitude
                    data.name = location.name
                    data.internal = True
                    match = True
                    break

        if match:
            return data
        else:
            return None
Example #16
0
    def netspeed_by_addr(self, addr):
        """
        Returns NetSpeed name from address.

        :arg addr: IP address (e.g. 203.0.113.30)
        """
        if self._databaseType == const.NETSPEED_EDITION:
            return const.NETSPEED_NAMES[self.id_by_addr(addr)]
        elif self._databaseType in (const.NETSPEED_EDITION_REV1,
                                    const.NETSPEED_EDITION_REV1_V6):
            ipnum = util.ip2long(addr)
            return self._get_org(ipnum)

        raise GeoIPError(
            'Invalid database type, expected NetSpeed or NetSpeedCell')
Example #17
0
    def netspeed_by_addr(self, addr):
        """
        Returns NetSpeed name from address.

        :arg addr: IP address (e.g. 203.0.113.30)
        """
        if self._databaseType == const.NETSPEED_EDITION:
            return const.NETSPEED_NAMES[self.id_by_addr(addr)]
        elif self._databaseType in (const.NETSPEED_EDITION_REV1,
                                    const.NETSPEED_EDITION_REV1_V6):
            ipnum = util.ip2long(addr)
            return self._get_org(ipnum)

        raise GeoIPError(
            'Invalid database type, expected NetSpeed or NetSpeedCell')
Example #18
0
    def id_by_addr(self, addr):
        """
        Returns the database ID for specified address.
        The ID might be useful as array index. 0 is unknown.

        :arg addr: IPv4 or IPv6 address (eg. 203.0.113.30)
        """
        ipv = 6 if addr.find(":") >= 0 else 4
        if ipv == 4 and self._databaseType not in (const.COUNTRY_EDITION, const.NETSPEED_EDITION):
            raise GeoIPError("Invalid database type; expected IPv6 address")
        if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
            raise GeoIPError("Invalid database type; expected IPv4 address")

        ipnum = util.ip2long(addr)
        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #19
0
    def time_zone_by_addr(self, addr):
        """
        Look up the time zone for a given IP address.
        Use this method if you have a Region or City database.

        @param addr: IP address
        @type addr: str
        @return: Time zone
        @rtype: str
        """
        if self._databaseType not in const.CITY_EDITIONS:
            message = 'Invalid database type, expected City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_record(ipnum).get('time_zone')
Example #20
0
    def region_by_addr(self, addr):
        """
        Lookup the region for given IP address.
        Use this method if you have a Region database.

        @param addr: IP address
        @type addr: str
        @return: Dictionary containing country_code and region_code
        @rtype: dict
        """
        if self._databaseType not in const.REGION_CITY_EDITIONS:
            message = 'Invalid database type, expected Region or City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_region(ipnum)
Example #21
0
    def time_zone_by_addr(self, addr):
        """
        Look up the time zone for a given IP address.
        Use this method if you have a Region or City database.

        @param addr: IP address
        @type addr: str
        @return: Time zone
        @rtype: str
        """
        if self._databaseType not in const.CITY_EDITIONS:
            message = 'Invalid database type, expected City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_record(ipnum).get('time_zone')
Example #22
0
    def region_by_addr(self, addr):
        """
        Lookup the region for given IP address.
        Use this method if you have a Region database.

        @param addr: IP address
        @type addr: str
        @return: Dictionary containing country_code and region_code
        @rtype: dict
        """
        if self._databaseType not in const.REGION_CITY_EDITIONS:
            message = 'Invalid database type, expected Region or City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_region(ipnum)
Example #23
0
    def org_by_addr(self, addr):
        """
        Lookup Organization, ISP or ASNum for given IP address.
        Use this method if you have an Organization, ISP or ASNum database.

        @param addr: IP address
        @type addr: str
        @return: organization or ISP name
        @rtype: str
        """
        valid = (const.ORG_EDITION, const.ISP_EDITION, const.ASNUM_EDITION, const.ASNUM_EDITION_V6)
        if self._databaseType not in valid:
            message = 'Invalid database type, expected Org, ISP or ASNum'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_org(ipnum)
Example #24
0
    def id_by_addr(self, addr):
        """
        Returns the database ID for specified address.
        The ID might be useful as array index. 0 is unknown.

        :arg addr: IPv4 or IPv6 address (eg. 203.0.113.30)
        """
        if self._databaseType in (const.PROXY_EDITION, const.NETSPEED_EDITION_REV1, const.NETSPEED_EDITION_REV1_V6):
            raise GeoIPError('Invalid database type; this database is not supported')
        ipv = 6 if addr.find(':') >= 0 else 4
        if ipv == 4 and self._databaseType not in (const.COUNTRY_EDITION, const.NETSPEED_EDITION):
            raise GeoIPError('Invalid database type; this database supports IPv6 addresses, not IPv4')
        if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
            raise GeoIPError('Invalid database type; this database supports IPv4 addresses, not IPv6')

        ipnum = util.ip2long(addr)
        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #25
0
    def org_by_addr(self, addr):
        """
        Lookup Organization, ISP or ASNum for given IP address.
        Use this method if you have an Organization, ISP or ASNum database.

        @param addr: IP address
        @type addr: str
        @return: organization or ISP name
        @rtype: str
        """
        valid = (const.ORG_EDITION, const.ISP_EDITION, const.ASNUM_EDITION,
                 const.ASNUM_EDITION_V6)
        if self._databaseType not in valid:
            message = 'Invalid database type, expected Org, ISP or ASNum'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        return self._get_org(ipnum)
Example #26
0
    def id_by_addr(self, addr):
        """
        Returns the database id for specified address.
        The id might be useful as array index. 0 is unknown.

        @param addr: IPv4 or IPv6 address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """
        ipv = 6 if addr.find(':') >= 0 else 4
        if ipv == 4 and self._databaseType not in (const.COUNTRY_EDITION, const.NETSPEED_EDITION):
            raise GeoIPError('Invalid database type; expected IPv6 address')
        if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
            raise GeoIPError('Invalid database type; expected IPv4 address')

        ipnum = util.ip2long(addr)
        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #27
0
    def record_by_addr(self, addr):
        """
        Returns dictionary with city data containing `country_code`, `country_name`,
        `region`, `city`, `postal_code`, `latitude`, `longitude`, `dma_code`,
        `metro_code`, `area_code`, `region_code` and `time_zone`.

        :arg addr: IP address (e.g. 203.0.113.30)
        """
        if self._databaseType not in const.CITY_EDITIONS:
            message = 'Invalid database type, expected City'
            raise GeoIPError(message)

        ipnum = util.ip2long(addr)
        rec = self._get_record(ipnum)
        if not rec:
            return None

        return rec
Example #28
0
    def _id_by_addr(self, addr):
        """
        Looks up the index for the country which is the key for the
        code and name.

        @param addr: IPv4 or IPv6 address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """
        ipv = 6 if addr.find(':') >= 0 else 4
        if ipv == 4 and self._databaseType != const.COUNTRY_EDITION:
            raise GeoIPError('Invalid database type; expected IPv6 address')
        if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
            raise GeoIPError('Invalid database type; expected IPv4 address')

        ipnum = util.ip2long(addr)
        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #29
0
    def id_by_addr(self, addr):
        """
        Returns the database id for specified address.
        The id might be useful as array index. 0 is unknown.

        @param addr: IPv4 or IPv6 address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """
        ipv = 6 if addr.find(':') >= 0 else 4
        if ipv == 4 and self._databaseType not in (const.COUNTRY_EDITION,
                                                   const.NETSPEED_EDITION):
            raise GeoIPError('Invalid database type; expected IPv6 address')
        if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
            raise GeoIPError('Invalid database type; expected IPv4 address')

        ipnum = util.ip2long(addr)
        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #30
0
    def id_by_addr(self, addr):
        """
        Get the country index.
        Looks up the index for the country which is the key for
        the code and name.

        @param addr: The IP address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """
        ipnum = util.ip2long(addr)
        if not ipnum:
            raise ValueError("Invalid IP address: %s" % addr)

        COUNTY_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)
        if self._databaseType not in COUNTY_EDITIONS:
            message = 'Invalid database type, expected Country'
            raise GeoIPError(message)

        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #31
0
    def id_by_addr(self, addr):
        """
        Get the country index.
        Looks up the index for the country which is the key for
        the code and name.

        @param addr: The IP address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """
        ipnum = util.ip2long(addr)
        if not ipnum:
            raise ValueError("Invalid IP address: %s" % addr)

        COUNTY_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)
        if self._databaseType not in COUNTY_EDITIONS:
            message = 'Invalid database type, expected Country'
            raise GeoIPError(message)

        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #32
0
    def region_by_addr(self, addr):
        """
        Lookup the region for given IP address.
        Use this method if you have a Region database.

        @param addr: IP address
        @type addr: str
        @return: Dictionary containing country_code, region and region_name
        @rtype: dict
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError('Invalid IP address')

            if self._databaseType not in const.REGION_CITY_EDITIONS:
                message = 'Invalid database type, expected Region or City'
                raise GeoIPError(message)

            return self._get_region(ipnum)
        except ValueError:
            raise GeoIPError('Failed to lookup address %s' % addr)
Example #33
0
    def time_zone_by_addr(self, addr):
        """
        Look up the time zone for a given IP address.
        Use this method if you have a Region or City database.

        @param addr: IP address
        @type addr: str
        @return: Time zone
        @rtype: str
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError('Invalid IP address')

            if self._databaseType not in const.CITY_EDITIONS:
                message = 'Invalid database type, expected City'
                raise GeoIPError(message)

            return self._get_record(ipnum)['time_zone']
        except ValueError:
            raise GeoIPError('Failed to lookup address %s' % addr)
Example #34
0
    def time_zone_by_addr(self, addr):
        """
        Look up the time zone for a given IP address.
        Use this method if you have a Region or City database.

        @param addr: IP address
        @type addr: str
        @return: Time zone
        @rtype: str
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError('Invalid IP address')

            if self._databaseType not in const.CITY_EDITIONS:
                message = 'Invalid database type, expected City'
                raise GeoIPError(message)

            return self._get_record(ipnum)['time_zone']
        except ValueError:
            raise GeoIPError('Failed to lookup address %s' % addr)
Example #35
0
    def region_by_addr(self, addr):
        """
        Lookup the region for given IP address.
        Use this method if you have a Region database.

        @param addr: IP address
        @type addr: str
        @return: Dictionary containing country_code, region and region_name
        @rtype: dict
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError('Invalid IP address')

            if self._databaseType not in const.REGION_CITY_EDITIONS:
                message = 'Invalid database type, expected Region or City'
                raise GeoIPError(message)

            return self._get_region(ipnum)
        except ValueError:
            raise GeoIPError('Failed to lookup address %s' % addr)
Example #36
0
    def id_by_addr(self, addr):
        """
        Get the country index.

        Looks up the index for the country which is the key for
        the code and name.

        @param addr: The IP address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """

        ipnum = ip2long(addr)

        if not ipnum:
            raise ValueError("Invalid IP address: %s" % addr)

        if self._databaseType != const.COUNTRY_EDITION:
            raise GeoIPError('Invalid database type; country_* methods expect '\
                             'Country database')

        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #37
0
    def org_by_addr(self, addr):
        """
        Lookup the organization (or ISP) for given IP address.
        Use this method if you have an Organization/ISP database.

        @param addr: IP address
        @type addr: str
        @return: organization or ISP name
        @rtype: str
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if self._databaseType not in (const.ORG_EDITION, const.ISP_EDITION, const.ASNUM_EDITION):
                raise GeoIPError('Invalid database type; org_* methods expect '\
                                 'Org/ISP database')

            return self._get_org(ipnum)
        except ValueError:
            raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % addr)
Example #38
0
    def id_by_addr(self, addr):
        """
        Get the country index.

        Looks up the index for the country which is the key for
        the code and name.

        @param addr: The IP address
        @type addr: str
        @return: network byte order 32-bit integer
        @rtype: int
        """

        ipnum = ip2long(addr)

        if not ipnum:
            raise ValueError("Invalid IP address: %s" % addr)

        if self._databaseType != const.COUNTRY_EDITION:
            raise GeoIPError('Invalid database type; country_* methods expect '\
                             'Country database')

        return self._seek_country(ipnum) - const.COUNTRY_BEGIN
Example #39
0
    def org_by_addr(self, addr):
        """
        Lookup Organization, ISP or ASNum for given IP address.
        Use this method if you have an Organization, ISP or ASNum database.

        @param addr: IP address
        @type addr: str
        @return: organization or ISP name
        @rtype: str
        """
        try:
            ipnum = util.ip2long(addr)
            if not ipnum:
                raise ValueError("Invalid IP address")

            valid = (const.ORG_EDITION, const.ISP_EDITION, const.ASNUM_EDITION)
            if self._databaseType not in valid:
                message = "Invalid database type, expected Org, ISP or ASNum"
                raise GeoIPError(message)

            return self._get_org(ipnum)
        except ValueError:
            raise GeoIPError("Failed to lookup address %s" % addr)
Example #40
0
    def time_zone_by_addr(self, addr):
        """
        Look up the time zone for a given IP address.
        Use this method if you have a Region or City database.

        @param addr: IP address
        @type addr: str
        @return: Time zone
        @rtype: str
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if not self._databaseType in (const.REGION_EDITION_REV0, const.REGION_EDITION_REV1,
                                          const.CITY_EDITION_REV0, const.CITY_EDITION_REV1):
                raise GeoIPError('Invalid database type; region_* methods expect '\
                                 'Region or City database')

            return self._get_record(ipnum)['time_zone']
        except ValueError:
            raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % addr)
Example #41
0
    def record_by_addr(self, addr):
        """
        Look up the record for a given IP address.
        Use this method if you have a City database.

        @param addr: IP address
        @type addr: str
        @return: dict with country_code, country_code3, country_name,
            region, city, postal_code, latitude, longitude,
            dma_code, metro_code, area_code, region_name, time_zone
        @rtype: dict
        """
        try:
            ipnum = ip2long(addr)

            if not ipnum:
                raise ValueError("Invalid IP address: %s" % addr)

            if not self._databaseType in (const.CITY_EDITION_REV0, const.CITY_EDITION_REV1):
                raise GeoIPError('Invalid database type; record_* methods expect City database')

            return self._get_record(ipnum)
        except ValueError:
            raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % addr)
Example #42
0
 def __init__(self, addr, mask, lat, lng, name):
     self.addr = ip2long(addr)
     self.mask = ip2long(mask)
     self.lat = lat
     self.long = lng
     self.name = name
Example #43
0
 def match(self, a):
     return (ip2long(a) & self.mask) == self.addr
Example #44
0
 def match(self, a):
     return (ip2long(a) & self.mask) == self.addr
Example #45
0
 def __init__(self, addr, mask, lat, lng, name):
     self.addr = ip2long(addr)
     self.mask = ip2long(mask)
     self.lat = lat
     self.long = lng
     self.name = name