本文整理汇总了Python中pygeoip.util.ip2long函数的典型用法代码示例。如果您正苦于以下问题:Python ip2long函数的具体用法?Python ip2long怎么用?Python ip2long使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ip2long函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: record_by_addr
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)
示例2: region_by_addr
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)
示例3: lookup
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
示例4: region_by_addr
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)
示例5: time_zone_by_addr
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')
示例6: org_by_addr
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)
示例7: netspeed_by_addr
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')
示例8: id_by_addr
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
示例9: lookup
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
示例10: time_zone_by_addr
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')
示例11: region_by_addr
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)
示例12: id_by_addr
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
示例13: org_by_addr
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)
示例14: record_by_addr
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
示例15: _id_by_addr
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