Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在whois包中扩展Python模块_Python_Module_Whois - Fatal编程技术网

在whois包中扩展Python模块

在whois包中扩展Python模块,python,module,whois,Python,Module,Whois,我正在使用pythonswhois模块检查.by区域中的免费域。模块当前不支持它。但我需要做的就是将这些代码添加到…/lib/python3.8/site-packages/whois/tld_regexpr.py中: by = { 'extend': 'com' } 我认为硬编码到lib文件夹是不对的。我的代码现在看起来是这样的: import whois def free_domains(domain_list): """Looking for free domains"""

我正在使用pythons
whois
模块检查
.by
区域中的免费域。模块当前不支持它。但我需要做的就是将这些代码添加到…/lib/python3.8/site-packages/whois/tld_regexpr.py中:

by = {
'extend': 'com'
}
我认为硬编码到lib文件夹是不对的。我的代码现在看起来是这样的:

import whois

def free_domains(domain_list):
    """Looking for free domains"""
    free_d = []
    for domain in domain_list:
        if whois.query(domain) is None:
            free_d.append(domain)
    return free_d

但是没有这些注射就不行 以下是的源代码供参考。在类似的情况下使用:

如我们所见,这运行一些模块级代码,从
tld_regexpr
中的数据生成正则表达式,并将它们缓存在
tld_RE
全局表中

令人烦恼的是,在这种情况发生之前,没有办法轻松地扩展
tld\u regexpr
,因为此模块是从顶级
\uuuuuu init\uuuuu.py
导入的。之后,内部代码甚至不再使用
get\u tld\u re
,尽管它提供了一个到缓存的接口:/因此,在添加新tld后,您需要在新tld上显式调用此
get\u tld\u re
。比如:

from whois import tld_regexpr
from whois._2_parse import get_tld_re  # a "private" module, but they leave us little choice
tld_regexpr.by = {
    'extend': 'com'
}
get_tld_re('by')
之前:

>>> whois.query('bayern.by')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/embray/src/python-whois/whois/__init__.py", line 60, in query
    raise UnknownTld('Unknown TLD: %s\n(all known TLD: %s)' % (tld, list(TLD_RE.keys())))
whois.exceptions.UnknownTld: Unknown TLD: by
(all known TLD: ['com', 'uk', 'ac_uk', 'ar', 'at', 'pl', 'be', 'biz', 'br', 'ca', 'cc', 'cl', 'club', 'cn', 'co', 'jp', 'co_jp', 'cz', 'de', 'edu', 'eu', 'fr', 'id', 'info', 'io', 'it', 'kr', 'kz', 'ru', 'lv', 'me', 'mobi', 'mx', 'name', 'net', 'nyc', 'nz', 'online', 'org', 'pharmacy', 'press', 'pw', 'store', 'rest', 'ru_rf', 'security', 'sh', 'site', 'space', 'tech', 'tel', 'theatre', 'tickets', 'tv', 'us', 'uz', 'video', 'website', 'wiki', 'xyz'])
whois.query('bayern.by') 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/home/embray/src/python whois/whois/_init__.py”,第60行,在查询中 引发UnknownTld('未知TLD:%s\n(所有已知TLD:%s)'(TLD,列表(TLD\u RE.keys())) whois.exceptions.UnknownTld:Unknown TLD:by (所有已知TLD:[‘com’、‘uk’、‘ac_-uk’、‘ar’、‘at’、‘pl’、‘be’、‘biz’、‘br’、‘ca’、‘cc’、‘cl’、‘club’、‘cn’、‘co’、‘jp’、‘co_-jp’、‘cz’、‘de’、‘edu’、‘eu’、‘fr’、‘id’、‘info’、‘io’、‘it’、‘kr’、‘kz’、‘ru’、‘lv’、‘me’、‘mobi’、‘mx’、‘name’、‘net’、‘net’、‘nycech’、‘电话’、‘剧院’、‘门票’、‘电视’、‘美国’、‘uz’、‘视频’、‘网站’、‘维基’、‘xyz’】 之后:

>>> from whois import tld_regexpr
>>> from whois._2_parse import get_tld_re  # a "private" module, but they leave us little choice
>>> tld_regexpr.by = {
...     'extend': 'com'
... }
>>> get_tld_re('by')
{'domain_name': re.compile('Domain Name:\\s?(.+)', re.IGNORECASE), 'registrar': re.compile('Registrar:\\s?(.+)', re.IGNORECASE), 'registrant': None, 'creation_date': re.compile('Creation Date:\\s?(.+)', re.IGNORECASE), 'expiration_date': re.compile('Registry Expiry Date:\\s?(.+)', re.IGNORECASE), 'updated_date': re.compile('Updated Date:\\s?(.+)$', re.IGNORECASE), 'name_servers': re.compile('Name Server:\\s*(.+)\\s*', re.IGNORECASE), 'status': re.compile('Status:\\s?(.+)', re.IGNORECASE), 'emails': re.compile('[\\w.-]+@[\\w.-]+\\.[\\w]{2,4}', re.IGNORECASE)}
>>> whois.query('bayern.by')
<whois._3_adjust.Domain object at 0x6ffffbbc9e8>
>>从whois导入tld\u regexpr
>>>来自whois。我们可以得到一个“私有”模块,但它们让我们别无选择
>>>tld_regexpr.by={
…'extend':'com'
... }
>>>获取(通过)
{'domain'u name':re.compile('domain:\\s?(.+)',re.IGNORECASE),'registrator':re.compile('registrator:\\s?(.+)',re.IGNORECASE),'registrator:\\s?(.+)',re.IGNORECASE),'registrator:\\s?(.IGNORECASE),'registrator:\\s?(.IGNORECASE),'updated:'re.compile('updated:'updated日期:\\s?(.IGNORECASE)$',re.IGNORECASE),'name\u servers':re.compile('name Server:\\s*(.+)\\s*,re.IGNORECASE),'status':re.compile('status:\\s?(.+)',re.IGNORECASE),'email':re.compile('[\\w.-]+[\\w.-]+\.\.[\\w]+\.[\\w]{2,4}',re.IGNORECASE)})
>>>whois.query('bayern.by')

我想模块的可扩展性设计不是最好的,但没关系——可以通过一些小的调整来修复。与此同时,你应该向作者提交一份PR,以添加更多的CCTLD,或者使可扩展性更容易。

导入
tld_regexpr
会导致导入
whois
本身,因此使用
tld_regexpr
在你修补它之前已经运行过了。是的,我已经解释过了,以及如何解决它。没关系;当我测试类似的东西时,我想重新导入
whois
以便运行
query
以某种方式重置数据。不确定我做了什么不同的事情,但
导入whois;whois.query(…)
在这个补丁之后似乎确实有效。添加了演示。
>>> from whois import tld_regexpr
>>> from whois._2_parse import get_tld_re  # a "private" module, but they leave us little choice
>>> tld_regexpr.by = {
...     'extend': 'com'
... }
>>> get_tld_re('by')
{'domain_name': re.compile('Domain Name:\\s?(.+)', re.IGNORECASE), 'registrar': re.compile('Registrar:\\s?(.+)', re.IGNORECASE), 'registrant': None, 'creation_date': re.compile('Creation Date:\\s?(.+)', re.IGNORECASE), 'expiration_date': re.compile('Registry Expiry Date:\\s?(.+)', re.IGNORECASE), 'updated_date': re.compile('Updated Date:\\s?(.+)$', re.IGNORECASE), 'name_servers': re.compile('Name Server:\\s*(.+)\\s*', re.IGNORECASE), 'status': re.compile('Status:\\s?(.+)', re.IGNORECASE), 'emails': re.compile('[\\w.-]+@[\\w.-]+\\.[\\w]{2,4}', re.IGNORECASE)}
>>> whois.query('bayern.by')
<whois._3_adjust.Domain object at 0x6ffffbbc9e8>