Python 使用'requests'检查域名是否已注册是否准确?

Python 使用'requests'检查域名是否已注册是否准确?,python,python-requests,urllib3,http.client,Python,Python Requests,Urllib3,Http.client,我注意到请求无效的urlrequests.get(无效的url)会引发以下异常: 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 o

我注意到请求无效的url
requests.get(无效的url)
会引发以下异常:

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)

没有;注册一个域并且没有根域名的IP地址是完全可以的,更不用说在该IP地址的端口80上运行服务器了。

正如@tripleee所提到的,这不是很精确。我使用python模块找到了另一种确定域名是否已注册的方法

要安装它

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。

我想检查域可用性的最佳方法是whois,而不是简单地尝试解析该名称。@CongMa我刚刚知道有一个名为的python模块。要安装它,
pip安装python whois
。据我所知,没有现有模块。您最好使用
requests
godaddy
上搜索域,因为DNS设置错误或其他网络问题可能会引发此异常。它们并不确切地意味着这样的域没有注册。@LittleQ python模块如何?有多个python whois客户端,但其中任何一个都应该用于检查域是否存在的简单情况。不同之处在于处理响应链以查找权威注册器,以及解析记录的通常不可机器读取的详细信息。它仍然不完美,因为一些TLD不支持whois,并迫使您使用他们自己的非标准查询系统,通常是一个自定义的基于web的作业。
#!/usr/bin/env python
import whois

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