Пример #1
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 (
                GEOIP.const.REGION_EDITION_REV0,
                GEOIP.const.REGION_EDITION_REV1,
                GEOIP.const.CITY_EDITION_REV0,
                GEOIP.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
            )
Пример #2
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 (GEOIP.const.CITY_EDITION_REV0, GEOIP.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
            )
Пример #3
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 (
                GEOIP.const.REGION_EDITION_REV0,
                GEOIP.const.REGION_EDITION_REV1,
                GEOIP.const.CITY_EDITION_REV0,
                GEOIP.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
            )
Пример #4
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 (GEOIP.const.REGION_EDITION_REV0,
                                          GEOIP.const.REGION_EDITION_REV1,
                                          GEOIP.const.CITY_EDITION_REV0,
                                          GEOIP.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)
Пример #5
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 (GEOIP.const.REGION_EDITION_REV0,
                                          GEOIP.const.REGION_EDITION_REV1,
                                          GEOIP.const.CITY_EDITION_REV0,
                                          GEOIP.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)
Пример #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: 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 (GEOIP.const.CITY_EDITION_REV0,
                                          GEOIP.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)
Пример #7
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 (GEOIP.const.ORG_EDITION,
                                          GEOIP.const.ISP_EDITION,
                                          GEOIP.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)
Пример #8
0
    def _lookup_country_id(self, addr):
        """
        Get the country index.

        This method is called by the _lookupCountryCode and _lookupCountryName
        methods. It looks up the index ('id') 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 != GEOIP.const.COUNTRY_EDITION:
            raise GeoIPError("Invalid database type; country_* methods expect " "Country database")

        return self._seek_country(ipnum) - GEOIP.const.COUNTRY_BEGIN
Пример #9
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 (GEOIP.const.ORG_EDITION, GEOIP.const.ISP_EDITION, GEOIP.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
            )
Пример #10
0
    def _lookup_country_id(self, addr):
        """
        Get the country index.

        This method is called by the _lookupCountryCode and _lookupCountryName
        methods. It looks up the index ('id') 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 != GEOIP.const.COUNTRY_EDITION:
            raise GeoIPError('Invalid database type; country_* methods expect '\
                             'Country database')

        return self._seek_country(ipnum) - GEOIP.const.COUNTRY_BEGIN