作为参考,这里是whois.tld_regexpr 的源代码。在whois._2_parse 中使用如下:
from . import tld_regexpr
TLD_RE = {}
def get_tld_re(tld):
if tld in TLD_RE:
return TLD_RE[tld]
v = getattr(tld_regexpr, tld)
extend = v.get('extend')
if extend:
e = get_tld_re(extend)
tmp = e.copy()
tmp.update(v)
else:
tmp = v
if 'extend' in tmp:
del tmp['extend']
TLD_RE[tld] = dict((k, re.compile(v, re.IGNORECASE) if isinstance(v, str) else v) for k, v in tmp.items())
return TLD_RE[tld]
[get_tld_re(tld) for tld in dir(tld_regexpr) if tld[0] != '_']
正如我们所见,这会运行一些模块级代码,从 tld_regexpr 中的数据生成正则表达式并将它们缓存在 TLD_RE 全局表中。
烦人的是,没有办法轻松扩展tld_regexpr 在发生这种情况之前,因为这个模块是从顶层__init__.py 导入的。然后,内部代码在此之后甚至不再使用get_tld_re,即使它提供了到缓存的接口:/ 所以你需要在添加它之后在你的新TLD 上显式地调用这个get_tld_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'])
之后:
>>> 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>
我猜这个模块没有最好的可扩展性设计,但没关系——可以通过一些小的调整来修复。同时,您应该向作者提交 PR 以添加更多 ccTLD,或使可扩展性更容易。