【问题标题】:What is the equivalent of PHP's geoip_isp_by_name function?PHP 的 geoip_isp_by_name 函数的等价物是什么?
【发布时间】:2013-04-18 19:21:31
【问题描述】:

我需要geoip_isp_by_name 的等价物。我正在使用Django GeoIP 模块。

我搜索了很多,但找不到该功能。请指教。

【问题讨论】:

    标签: python django geoip


    【解决方案1】:

    pygeoip 支持使用 GeoIP 数据库进行 ISP 查找。

    # Normally found under /usr/share/GeoIP/{GeoIP,GeoIPv6}.dat
    gi = pygeoip.GeoIP('/path/to/GeoIPISP.dat')
    gi.org_by_name('cnn.com')
    'Turner Broadcasting System'
    

    GeoIP 数据库的安装过程取决于您的操作系统和发行版。它通常在您最喜欢的发行版存储库中的geoip-database 下可用。欲了解更多信息,请查看GeoIP Country Database Installation Instructions

    遗憾的是,我无法让它工作,我在安装 Ubuntu 12.10 和 Debian 6 时都遇到了错误,但我不确定我的 GeoIP 数据库是否损坏或模块损坏。你可能会有更好的运气。

    gi.org_by_name("cnn.com")
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/pygeoip/__init__.py", line 548, in org_by_name
        return self.org_by_addr(addr)
      File "/usr/local/lib/python2.7/dist-packages/pygeoip/__init__.py", line 531, in org_by_addr
        raise GeoIPError(message)
    GeoIPError: Invalid database type, expected Org, ISP or ASNum
    

    更新:由于 GeoIP 无法正常工作(请参阅 cmets),我尝试使用这些跟踪站点。得到了一个适用于 IP 和主机名的 hacky 解决方案。可能不应该用作永久解决方案,但它工作得很好。

    import html2text
    import re
    import urllib2
    
    lookup = "thevoid.no" # accepts both hostname and ip
    
    tracer = "http://www.ip-adress.com/ip_tracer/"
    pat    = "ISP of this IP \[\?\]:\n\n([a-zA-Z ]+)"
    hdr    = {'User-Agent': 'Mozilla/5.0'} # ip-adress.com doesn't accept Python
    req    = urllib2.Request(tracer + lookup, headers=hdr)
    page   = urllib2.urlopen(req).read()
    
    h = html2text.HTML2Text()
    h.ignore_links = True
    text = h.handle(page)
    
    try:
        # Hetzner Online AG
        print re.search(pat, text).group(1)
    except:
        print "Could not find ISP for", lookup
    

    更新 2:收到了 answer 来解决我的问题。有seperate GeoIP databasesOrg edition 可以在这里找到,但似乎要花钱。我不确定 Python 中是否有免费的替代品,或者有人共享他们的 API。如果没有,我的 hacky 解决方案可能就是它。

    【讨论】:

    • 我在他们的Github repo 上创建了一个新的issue。如果/当我得到答案时,我会更新我的答案。
    • @AdamSilver 我添加了一个 hacky 替代解决方案。看看它是否适合您的用例。
    猜你喜欢
    • 2011-12-25
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多