您建议使用哪种 Python 方式来检查 whois 数据库记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What Python way would you suggest to check whois database records?
提问by kender
I'm trying to get a webservice up and running that actually requires to check whois databases. What I'm doing right now is ugly and I'd like to avoid it as much as I can: I call gwhois command and parse its output. Ugly.
我正在尝试启动并运行一个实际上需要检查 whois 数据库的网络服务。我现在正在做的事情很丑陋,我想尽可能避免它:我调用 gwhois 命令并解析它的输出。丑陋的。
I did some search to try to find a pythonic way to do this task. Generally I got quite much nothing - this old discussion list linkhas a way to check if domain exist. Quite not what I was looking for... But still, it was best anwser Google gave me - everything else is just a bunch of unanwsered questions.
我做了一些搜索,试图找到一种 Pythonic 方式来完成这项任务。一般来说,我几乎一无所获——这个旧的讨论列表链接有一种方法可以检查域是否存在。完全不是我想要的......但是,它仍然是谷歌给我的最好的答案 - 其他一切都只是一堆未回答的问题。
Any of you have succeeded to get some method up and running? I'd very much appreciate some tips, or should I just do it the opensource-way, sit down and code something by myself? :)
你们中有人成功地启动并运行了一些方法吗?我非常感谢一些提示,或者我应该以开源方式来做,坐下来自己编写代码?:)
采纳答案by cdleary
There's nothing wrong with using a command line utility to do what you want. If you put a nice wrapper around the service, you can implement the internals however you want! For example:
使用命令行实用程序执行您想要的操作并没有错。如果您在服务周围放置了一个很好的包装器,则可以根据需要实现内部结构!例如:
class Whois(object):
_whois_by_query_cache = {}
def __init__(self, query):
"""Initializes the instance variables to defaults. See :meth:`lookup`
for details on how to submit the query."""
self.query = query
self.domain = None
# ... other fields.
def lookup(self):
"""Submits the `whois` query and stores results internally."""
# ... implementation
Now, whether or not you roll your own using urllib, wrap around a command line utility (like you're doing), or import a third party library and use that (like you're saying), this interface stays the same.
现在,无论您是否使用 urllib 自己制作,环绕命令行实用程序(就像您正在做的那样),或者导入第三方库并使用它(就像您说的那样),这个界面都保持不变。
This approach is generally not considered ugly at all -- sometimes command utilities do what you want and you should be able to leverage them. If speed ends up being a bottleneck, your abstraction makes the process of switching to a native Python implementation transparent to your client code.
这种方法通常根本不被认为是丑陋的——有时命令实用程序可以做您想做的事情,您应该能够利用它们。如果速度最终成为瓶颈,您的抽象使切换到本机 Python 实现的过程对您的客户端代码透明。
Practicality beats purity-- that's what's Pythonic. :)
实用性胜过纯粹——这就是 Pythonic。:)
回答by Aziz
Look at this: http://code.google.com/p/pywhois/
看看这个:http: //code.google.com/p/pywhois/
pywhois - Python module for retrieving WHOIS information of domains
pywhois - 用于检索域的 WHOIS 信息的 Python 模块
Goal: - Create a simple importable Python module which will produce parsed WHOIS data for a given domain. - Able to extract data for all the popular TLDs (com, org, net, ...) - Query a WHOIS server directly instead of going through an intermediate web service like many others do. - Works with Python 2.4+ and no external dependencies
目标: - 创建一个简单的可导入 Python 模块,该模块将为给定域生成解析的 WHOIS 数据。- 能够提取所有流行 TLD(com、org、net 等)的数据 - 直接查询 WHOIS 服务器,而不是像许多其他人一样通过中间网络服务。- 适用于 Python 2.4+ 且无外部依赖项
Example:
例子:
>>> import pywhois
>>> w = pywhois.whois('google.com')
>>> w.expiration_date
['14-sep-2011']
>>> w.emails
['[email protected]',
'[email protected]',
'[email protected]',
'[email protected]']
>>> print w
...
回答by Lars Nordin
Found this question in the process of my own search for a python whois library.
在我自己搜索python whois库的过程中发现了这个问题。
Don't know that I agree with cdleary's answer that using a library that wraps a command is always the best way to go - but I can see his reasons why he said this.
不知道我是否同意 cdleary 的回答,即使用包装命令的库始终是最好的方法 - 但我可以理解他为什么这么说的原因。
Pro: cmd-line whois handles all the hard work (socket calls, parsing, etc)
优点:cmd-line whois 处理所有繁重的工作(套接字调用、解析等)
Con: not portable; module may not work depending on underlying whois command. Slower, since running a command and most likely shell in addition to whois command. Affected if not UNIX (Windows), different UNIX, older UNIX, or older whois command
缺点:不便携;模块可能无法工作,具体取决于底层 whois 命令。较慢,因为除了 whois 命令之外,还运行命令和最有可能的 shell。如果不是 UNIX (Windows)、不同的 UNIX、较旧的 UNIX 或较旧的 whois 命令,则受影响
I am looking for a whois module that can handle whois IP lookups and I am not interested in coding my own whois client.
我正在寻找可以处理 whois IP 查找的 whois 模块,但我对编写自己的 whois 客户端不感兴趣。
Here are the modules that I (lightly) tried out and more information about it:
以下是我(轻微)尝试过的模块以及有关它的更多信息:
pywhoisapi:
pywhoisapi:
- Home: http://code.google.com/p/pywhoisapi/
- Design: REST client accessing ARIN whois REST service
- Pros: Able to handle IP address lookups
- Cons: Able to pull information from whois servers of other RIRs?
- 主页:http: //code.google.com/p/pywhoisapi/
- 设计:REST 客户端访问 ARIN whois REST 服务
- 优点:能够处理 IP 地址查找
- 缺点:能够从其他 RIR 的 whois 服务器中提取信息吗?
BulkWhois
批量Whois
- Home: http://pypi.python.org/pypi/BulkWhois/0.2.1
- Design: telnet client accessing whois telnet query interface from RIR(?)
- Pros: Able to handle IP address lookups
- Cons: Able to pull information from whois servers of other RIRs?
- 主页:http: //pypi.python.org/pypi/BulkWhois/0.2.1
- 设计:telnet 客户端从 RIR(?) 访问 whois telnet 查询接口
- 优点:能够处理 IP 地址查找
- 缺点:能够从其他 RIR 的 whois 服务器中提取信息吗?
pywhois:
pywhois:
- Home: http://code.google.com/p/pywhois/
- Design: REST client accessing RRID whois services
- Pros: Accessses many RRIDs; has python 3.x branch
- Cons: does not seem to handle IP address lookups
- 主页:http: //code.google.com/p/pywhois/
- 设计:REST 客户端访问 RRID whois 服务
- 优点:访问许多 RRID;有 python 3.x 分支
- 缺点:似乎不处理 IP 地址查找
python-whois:
python-whois:
- Home: http://code.google.com/p/python-whois/
- Design: wraps "whois" command
- Cons: does not seem to handle IP address lookups
- 主页:http: //code.google.com/p/python-whois/
- 设计:包装“whois”命令
- 缺点:似乎不处理 IP 地址查找
whoisclient - fork of python-whois
whoisclient - python-whois 的分支
- Home: http://gitorious.org/python-whois
- Design: wraps "whois" command
- Depends on: IPy.py
- Cons: does not seem to handle IP address lookups
- 主页:http: //gitorious.org/python-whois
- 设计:包装“whois”命令
- 取决于:IPy.py
- 缺点:似乎不处理 IP 地址查找
Update: I ended up using pywhoisapi for the reverse IP lookups that I was doing
更新:我最终使用 pywhoisapi 进行我正在做的反向 IP 查找
回答by Boden Garman
Here is the whois client re-implemented in Python: http://code.activestate.com/recipes/577364-whois-client/
这是用 Python 重新实现的 whois 客户端:http: //code.activestate.com/recipes/577364-whois-client/
回答by tzot
I don't know if gwhois does something special with the server output; however, you can plainly connect to the whois server on port whois (43), send your query, read all the data in the reply and parse them. To make life a little easier, you could use the telnetlib.Telnet class (even if the whois protocol is much simpler than the telnet protocol) instead of plain sockets.
我不知道 gwhois 是否对服务器输出做了一些特别的处理;但是,您可以通过 whois (43) 端口直接连接到 whois 服务器,发送您的查询,读取回复中的所有数据并解析它们。为了让生活更轻松一些,您可以使用 telnetlib.Telnet 类(即使 whois 协议比 telnet 协议简单得多)而不是普通套接字。
The tricky parts:
棘手的部分:
- which whois server will you ask? RIPE, ARIN, APNIC, LACNIC, AFRINIC, JPNIC, VERIO etc LACNIC could be a useful fallback, since they tend to reply with useful data to requests outside of their domain.
- what are the exact options and arguments for each whois server? some offer help, others don't. In general, plain domain names work without any special options.
- 你会问哪个whois服务器?RIPE、ARIN、APNIC、LACNIC、AFRINIC、JPNIC、VERIO 等 LACNIC 可能是一个有用的回退,因为它们倾向于用有用的数据回复其域外的请求。
- 每个 whois 服务器的确切选项和参数是什么?有些提供帮助,有些则不提供。通常,纯域名无需任何特殊选项即可使用。
回答by Ryan
import socket
socket.gethostbyname_ex('url.com')
if it returns a gaierror you know know it's not registered with any DNS
如果它返回一个 gaierror 你知道它没有注册任何 DNS
回答by flow
here is a ready-to-use solution that works for me; written for Python 3.1 (when backporting to Py2.x, take special care of the bytes / Unicode text distinctions). your single point of access is the method DRWHO.whois()
, which expects a domain name to be passed in; it will then try to resolve the name using the provider configured as DRWHO.whois_providers[ '*' ]
(a more complete solution could differentiate providers according to the top level domain). DRWHO.whois()
will return a dictionary with a single entry text
, which contains the response text sent back by the WHOIS server. Again, a more complete solution would then try and parse the text (which must be done separately for each provider, as there is no standard format) and return a more structured format (e.g., set a flag available
which specifies whether or not the domain looks available). have fun!
这是一个对我有用的即用型解决方案;为 Python 3.1 编写(当向后移植到 Py2.x 时,要特别注意字节 / Unicode 文本的区别)。您的单点访问是方法DRWHO.whois()
,它期望传入一个域名;然后它将尝试使用配置为的提供程序解析名称DRWHO.whois_providers[ '*' ]
(更完整的解决方案可以根据顶级域区分提供程序)。DRWHO.whois()
将返回一个text
包含单个条目的字典,其中包含 WHOIS 服务器发回的响应文本。同样,一个更完整的解决方案将尝试解析文本(必须为每个提供者单独完成,因为没有标准格式)并返回一个更结构化的格式(例如,设置一个标志available
它指定域是否看起来可用)。玩得开心!
##########################################################################
import asyncore as _sys_asyncore
from asyncore import loop as _sys_asyncore_loop
import socket as _sys_socket
##########################################################################
class _Whois_request( _sys_asyncore.dispatcher_with_send, object ):
# simple whois requester
# original code by Frederik Lundh
#-----------------------------------------------------------------------
whoisPort = 43
#-----------------------------------------------------------------------
def __init__(self, consumer, host, provider ):
_sys_asyncore.dispatcher_with_send.__init__(self)
self.consumer = consumer
self.query = host
self.create_socket( _sys_socket.AF_INET, _sys_socket.SOCK_STREAM )
self.connect( ( provider, self.whoisPort, ) )
#-----------------------------------------------------------------------
def handle_connect(self):
self.send( bytes( '%s\r\n' % ( self.query, ), 'utf-8' ) )
#-----------------------------------------------------------------------
def handle_expt(self):
self.close() # connection failed, shutdown
self.consumer.abort()
#-----------------------------------------------------------------------
def handle_read(self):
# get data from server
self.consumer.feed( self.recv( 2048 ) )
#-----------------------------------------------------------------------
def handle_close(self):
self.close()
self.consumer.close()
##########################################################################
class _Whois_consumer( object ):
# original code by Frederik Lundh
#-----------------------------------------------------------------------
def __init__( self, host, provider, result ):
self.texts_as_bytes = []
self.host = host
self.provider = provider
self.result = result
#-----------------------------------------------------------------------
def feed( self, text ):
self.texts_as_bytes.append( text.strip() )
#-----------------------------------------------------------------------
def abort(self):
del self.texts_as_bytes[:]
self.finalize()
#-----------------------------------------------------------------------
def close(self):
self.finalize()
#-----------------------------------------------------------------------
def finalize( self ):
# join bytestrings and decode them (witha a guessed encoding):
text_as_bytes = b'\n'.join( self.texts_as_bytes )
self.result[ 'text' ] = text_as_bytes.decode( 'utf-8' )
##########################################################################
class DRWHO:
#-----------------------------------------------------------------------
whois_providers = {
'~isa': 'DRWHO/whois-providers',
'*': 'whois.opensrs.net', }
#-----------------------------------------------------------------------
def whois( self, domain ):
R = {}
provider = self._get_whois_provider( '*' )
self._fetch_whois( provider, domain, R )
return R
#-----------------------------------------------------------------------
def _get_whois_provider( self, top_level_domain ):
providers = self.whois_providers
R = providers.get( top_level_domain, None )
if R is None:
R = providers[ '*' ]
return R
#-----------------------------------------------------------------------
def _fetch_whois( self, provider, domain, pod ):
#.....................................................................
consumer = _Whois_consumer( domain, provider, pod )
request = _Whois_request( consumer, domain, provider )
#.....................................................................
_sys_asyncore_loop() # loops until requests have been processed
#=========================================================================
DRWHO = DRWHO()
domain = 'example.com'
whois = DRWHO.whois( domain )
print( whois[ 'text' ] )
回答by Justin Standard
Another way to do it is to use urllib2
module to parse some other page's whois service (many sites like that exist). But that seems like even more of a hack that what you do now, and would give you a dependency on whatever whois site you chose, which is bad.
另一种方法是使用urllib2
模块来解析其他页面的 whois 服务(存在许多类似的站点)。但这似乎比您现在所做的更像是一种黑客行为,并且会让您依赖于您选择的任何 whois 站点,这很糟糕。
I hate to say it, but unless you want to re-implement whois
in your program (which would be re-inventing the wheel), running whois
on the OS and parsing the output (ie what you are doing now) seems like the right way to do it.
我不想这么说,但除非你想whois
在你的程序中重新实现(这将重新发明轮子),whois
在操作系统上运行并解析输出(即你现在正在做什么)似乎是正确的方法做吧。
回答by kender
Parsing another webpage woulnd't be as bad (assuming their html woulnd't be very bad), but it would actually tie me to them - if they're down, I'm down :)
解析另一个网页不会那么糟糕(假设他们的 html 不会很糟糕),但它实际上会将我与他们联系起来 - 如果他们失败了,我就失败了 :)
Actually I found some old project on sourceforge: rwhois.py. What scares me a bit is that their last update is from 2003. But, it might seem as a good place to start reimplementation of what I do right now... Well, I felt obligued to post the link to this project anyway, just for further reference.
实际上我在 sourceforge 上发现了一些旧项目:rwhois.py。让我有点害怕的是他们的最后一次更新是从 2003 年开始的。但是,这似乎是重新实现我现在所做的事情的好地方......好吧,无论如何我觉得有必要发布这个项目的链接,只是以供进一步参考。