如何在Python 3中通过IP获取WhoIs信息?

24

注意:这不是一篇图书馆推荐问题,而是关于可能的方法来从给定的IP地址在 Python 3 中检索 WhoIs 信息。

问题:有哪些方法可以在 Python 3 中从给定的 IP 地址中检索 WhoIs 信息?结果应至少包含以下内容:

  • 注册国家
  • ISP名称、地址、滥用电子邮件
  • 如果已注册,则为域名
  • 注册和到期日期
  • 如果结果不是纯文本且以上信息是结构化的,则奖励分

我不想要外壳“whois”命令的包装程序,因为该程序必须在 Windows 下工作。

在提出这个问题之前,谷歌给了我以下图书馆:

当安装通过 pip 或导入期间时,以下内容会引发错误:

  • BulkWhois
  • WhoisClient
  • cymruwhois
  • dwhois
  • ipwhois
  • pyiptools
  • python-whois
  • pywhois
  • uwhoisd
  • whois
  • whoislookup
  • whoispy

以下图书馆在 Python 3 中确实起作用,但它们做的是我想要的相反 - 它们按域名查找,而不是按 IP 地址查找:

  • nicnames
  • pythonwhois

在提问之前,我已经研究了以下问题:


嗯,是的。我有点困惑你在问什么,如果不是关于库或服务的话。我花了一会儿时间在谷歌上搜索,找到了几个RESTy/JSONy服务。 - Michael Petrotta
所以你基本上想要复制whois命令的功能?源代码并不是特别长,所以你可以从这里开始:whois.c。这里有一个Python示例:链接 - user3557327
@MichaelPetrotta 没错!这就是问题所在——你描述的(不同)方法是手动解析在线服务的响应。如果你在这里将你的评论写成答案,并建议一些免费的JSON/REST服务,我会接受你的答案。(我查看了谷歌搜索“whois json”的前10个结果,它们都是付费的,或者只能通过域名进行查询。) - Babken Vardanyan
uwhoisd目前不支持IP查找,但这是我想要添加的功能。安装时您遇到了什么错误?目前文档还不够完善,但我希望能尽快解决这个问题。 - Keith Gaughan
@KeithGaughan http://pastie.org/10823660 - Babken Vardanyan
显示剩余5条评论
2个回答

31

here 安装 dnspython 的稳定版本。

然后执行 pip3 install ipwhois 命令。

In [37]: from ipwhois import IPWhois

In [38]: obj = IPWhois('74.125.225.229')

In [39]: res=obj.lookup()

In [40]: res["nets"][0]['country']
Out[40]: 'US'

In [41]: res["nets"][0]['abuse_emails']
Out[41]: 'arin-contact@google.com'

In [42]: from pprint import pprint

In [43]: pprint(res)
{'asn': '15169',
 'asn_cidr': '74.125.225.0/24',
 'asn_country_code': 'US',
 'asn_date': '2007-03-13',
 'asn_registry': 'arin',
 'nets': [{'abuse_emails': 'arin-contact@google.com',
           'address': '1600 Amphitheatre Parkway',
           'cidr': '74.125.0.0/16',
           'city': 'Mountain View',
           'country': 'US',
           'created': '2007-03-13T00:00:00',
           'description': 'Google Inc.',
           'misc_emails': None,
           'name': 'GOOGLE',
           'postal_code': '94043',
           'state': 'CA',
           'tech_emails': 'arin-contact@google.com',
           'updated': '2012-02-24T00:00:00'}],
 'query': '74.125.225.229',
 'raw': None}

HTTP:

In [44]: res=obj.lookup_rws()

In [45]: pprint(res)
{'asn': '15169',
 'asn_cidr': '74.125.225.0/24',
 'asn_country_code': 'US',
 'asn_date': '2007-03-13',
 'asn_registry': 'arin',
 'nets': [{'abuse_emails': 'arin-contact@google.com',
           'address': '1600 Amphitheatre Parkway',
           'cidr': '74.125.0.0/16',
           'city': 'Mountain View',
           'country': 'US',
           'created': '2007-03-13T12:09:54-04:00',
           'description': 'Google Inc.',
           'misc_emails': None,
           'name': 'GOOGLE',
           'postal_code': '94043',
           'state': 'CA',
           'tech_emails': 'arin-contact@google.com',
           'updated': '2012-02-24T09:44:34-05:00'}],
 'query': '74.125.225.229',
 'raw': None}

API已更改,对于legacy ipwhoisIPWhois.lookup()在v0.12.0中已弃用并将被删除。传统的whois查询已移至IPWhois.lookup_whois()。
您可以访问该方法,我已禁用警告以便查看输出,在实际使用情况中应考虑到已弃用的警告。
In [30]: from warnings import filterwarnings

In [31]: filterwarnings( action="ignore")

In [32]: from ipwhois import IPWhois

In [33]: obj = IPWhois('74.125.225.229')

In [34]: obj.lookup_whois()
Out[34]: 
{'asn': '15169',
 'asn_cidr': '74.125.225.0/24',
 'asn_country_code': 'US',
 'asn_date': '2007-03-13',
 'asn_description': 'GOOGLE - Google Inc., US',
 'asn_registry': 'arin',
 'nets': [{'address': '1600 Amphitheatre Parkway',
   'cidr': '74.125.0.0/16',
   'city': 'Mountain View',
   'country': 'US',
   'created': '2007-03-13',
   'description': 'Google Inc.',
   'emails': ['arin-contact@google.com', 'network-abuse@google.com'],
   'handle': 'NET-74-125-0-0-1',
   'name': 'GOOGLE',
   'postal_code': '94043',
   'range': '74.125.0.0 - 74.125.255.255',
   'state': 'CA',
   'updated': '2012-02-24'}],
 'nir': None,
 'query': '74.125.225.229',
 'raw': None,
 'raw_referral': None,
 'referral': None}

The docs指出,IPWhois.lookup_rdap()现在是推荐的查询方法。RDAP提供了比传统whois和REST查询(以前的实现)更好的数据结构。RDAP查询允许解析用户、组织和团体的联系信息和详细信息。RDAP还提供了更详细的网络信息。 但是,按照usage example的用法或添加asn_methods=["whois"]仍会产生弃用警告,因此这是实际使用情况中需要解决的问题。
In [31]: from ipwhois import IPWhois

In [32]: obj = IPWhois('74.125.225.229')
/usr/local/lib/python3.6/site-packages/ipwhois/net.py:138: UserWarning: allow_permutations has been deprecated and will be removed. It is no longer needed, due to the deprecation of asn_alts, and the addition of the asn_methods argument.
  warn('allow_permutations has been deprecated and will be removed. '

In [33]:  obj.lookup_rdap(asn_methods=["whois"])
/usr/local/lib/python3.6/site-packages/ipwhois/asn.py:302: UserWarning: IPASN._parse_fields_whois() has been deprecated and will be removed. You should now use IPASN.parse_fields_whois().
  warn('IPASN._parse_fields_whois() has been deprecated and will be '
Out[33]: 
{'asn': '15169',
 'asn_cidr': '74.125.225.0/24',
 'asn_country_code': 'US',
 'asn_date': '2007-03-13',
 'asn_description': 'GOOGLE - Google Inc., US',
 'asn_registry': 'arin',
 'entities': ['GOGL'],
 'network': {'cidr': '74.125.0.0/16',
  'country': None,
  'end_address': '74.125.255.255',
  'events': [{'action': 'last changed',
    'actor': None,
    'timestamp': '2012-02-24T09:44:34-05:00'},
   {'action': 'registration',
    'actor': None,
    'timestamp': '2007-03-13T12:09:54-04:00'}],
  'handle': 'NET-74-125-0-0-1',
  'ip_version': 'v4',
  'links': ['https://rdap.arin.net/registry/ip/074.125.000.000',
   'https://whois.arin.net/rest/net/NET-74-125-0-0-1'],
  'name': 'GOOGLE',
  'notices': [{'description': 'By using the ARIN RDAP/Whois service, you are agreeing to the RDAP/Whois Terms of Use',
    'links': ['https://www.arin.net/whois_tou.html'],
    'title': 'Terms of Service'}],
  'parent_handle': 'NET-74-0-0-0-0',
  'raw': None,
  'remarks': None,
  'start_address': '74.125.0.0',
  'status': None,
  'type': None},
 'nir': None,
 'objects': {'GOGL': {'contact': {'address': [{'type': None,
      'value': '1600 Amphitheatre Parkway\nMountain View\nCA\n94043\nUNITED STATES'}],
    'email': None,
    'kind': 'org',
    'name': 'Google Inc.',
    'phone': None,
    'role': None,
    'title': None},
   'entities': ['ABUSE5250-ARIN', 'ZG39-ARIN'],
   'events': [{'action': 'last changed',
     'actor': None,
     'timestamp': '2017-01-28T08:32:29-05:00'},
    {'action': 'registration',
     'actor': None,
     'timestamp': '2000-03-30T00:00:00-05:00'}],
   'events_actor': None,
   'handle': 'GOGL',
   'links': ['https://rdap.arin.net/registry/entity/GOGL',
    'https://whois.arin.net/rest/org/GOGL'],
   'notices': None,
   'raw': None,
   'remarks': None,
   'roles': ['registrant'],
   'status': None}},
 'query': '74.125.225.229',
 'raw': None}

1
我可以直接使用pip3 install dnspython3 :) - Babken Vardanyan
3
属性错误:'IPWhois'对象没有属性'lookup'。请改用 ipwhois_obj.lookup_whois()ipwhois_obj.lookup_rdap() - gies0r
1
@gies0r,你可以使用lookup_whois()函数。lookup函数已经在一段时间内弃用并在更近的版本中被删除。即使使用最新的示例,仍然会出现警告。因此,在Github上开启一个问题可能值得跟进。 - Padraic Cunningham

5

现在可使用的 Python 3 的替代简单解决方案:

首先导入以下包:

pip install python-whois

然后执行这段代码:

import whois
w = whois.whois('74.125.225.229')

我收到了AttributeError: module 'whois' has no attribute 'whois'的错误信息。 - 400 the Cat
1
pip uninstall whois pip install python-whois - Fabio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接