2015-07-21 80 views
1

我注意到,请求无效的URL requests.get(invalid_url)抛出以下例外:使用`requests`来检查域名是否被注册是否准确?

Traceback (most recent call last): 
    File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo 
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags): 
socket.gaierror: [Errno -2] Name or service not known 

During handling of the above exception, another exception occurred: 
    File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 607, in urlopen 
    raise MaxRetryError(self, url, e) 
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='sparkandshine.me', port=80): Max retries exceeded with url:/(Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known) 

During handling of the above exception, another exception occurred: 
    File "/usr/lib/python3/dist-packages/requests/adapters.py", line 378, in send 
    raise ConnectionError(e) 
requests.exceptions.ConnectionError: HTTPConnectionPool(host='sparkandshine.me', port=80): Max retries exceeded with url:/(Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known) 

是否精确确定一个域名是否被捕获这些例外注册与否?这里是源代码:

#!/usr/bin/env python3 
import http 
import urllib3 
import requests 

url = 'http://example.com' 
try : 
    r = requests.get(url) 
except (http.client.HTTPException, urllib3.exceptions.MaxRetryError, requests.exceptions.ConnectionError): 
    print(url) #this domain name is not registered? 
+0

我想,检查域可用性的最好方法是whois,而不是简单地试图解决这个名称。 –

+0

@CongMa我刚刚知道有一个名为[pywhois]的python模块(https://bitbucket.org/richardpenman/pywhois)。要安装它,请'安装python-whois'。 – SparkAndShine

+0

我知道没有现有的模块。您最好使用'request'在'godaddy'上搜索域名。 – LittleQ

回答

4

否;对域进行注册并且没有根域名的IP地址是非常好的,更不用说让服务器在该IP地址的端口80上运行。

3

正如@tripleee所说,它不是很精确。我找到另一种方法来确定域名是否已注册,使用Python模块pywhois

要安装它,

pip install python-whois 

下面是一个例子。

#!/usr/bin/env python 
import whois 

url = 'example.com' 
try : 
    w = whois.whois(url) 
except (whois.parser.PywhoisError): 
    print(url) 

PS:不支持python3。

+1

这里有多个Python whois客户端,但他们中的任何一个都应该用于检查域是否存在的简单情况。区别在于处理响应链以找到权威注册服务器并解析通常不是机器可读的记录细节它仍然不完美,因为一些TLD不支持whois,并且迫使你使用自己的非标准查询系统,通常是基于web的自定义作业。 – tripleee

相关问题