本文整理汇总了Python中pygeoip.GeoIP.asn_by_addr方法的典型用法代码示例。如果您正苦于以下问题:Python GeoIP.asn_by_addr方法的具体用法?Python GeoIP.asn_by_addr怎么用?Python GeoIP.asn_by_addr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygeoip.GeoIP
的用法示例。
在下文中一共展示了GeoIP.asn_by_addr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import asn_by_addr [as 别名]
def index():
res_data = {}
verbose = False
prettyprint = False
try:
client_ip = request.remote_route[0]
except:
client_ip = request.environ['REMOTE_ADDR']
pass
args = request.query_string.split('&')
if 'v' in args:
verbose = True
if 'pp' in args:
prettyprint = True
res_data['ip'] = client_ip
try:
ua = request.environ['HTTP_USER_AGENT']
user_agent = user_agents.parse(ua)
res_data['user_agent'] = str(user_agent)
except Exception as e:
user_agent = False
pass
if user_agent and not verbose:
if user_agent.browser.family == 'Other':
response.set_header('Content-type', 'text/plain')
return res_data['ip'] + '\r\n'
try:
ip = GeoIP(config.get('app', 'geoip_database'))
res_data['country'] = ip.country_name_by_addr(client_ip)
res_data['city'] = ip.record_by_addr(client_ip)
res_data['asn'] = ip.asn_by_addr(client_ip)
except Exception as e:
pass
try:
name = reversename.from_address(client_ip)
answers = resolver.query(name, 'PTR')
res_data['hostname'] = []
for answer in answers:
res_data['hostname'].append(str(answer).rstrip('.'))
except Exception as e:
pass
if user_agent and verbose:
if user_agent.browser.family == 'Other':
response.set_header('Content-type', 'application/json')
if prettyprint:
return json.dumps(
res_data,
sort_keys=True,
indent=4,
separators=(',', ': ')
) + '\r\n'
return json.dumps(res_data)
return template(
'index',
ua_info=res_data,
page_title='Your IP-address is: ' + res_data['ip'],
verbose=verbose
)