当前位置: 首页>>代码示例>>Python>>正文


Python GeoIP.asn_by_addr方法代码示例

本文整理汇总了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
    )
开发者ID:stemid,项目名称:ipa.sh,代码行数:70,代码来源:app.py


注:本文中的pygeoip.GeoIP.asn_by_addr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。