使用`requests`检查域名是否被注册是否准确?
Is it precise to use `requests` to check if a domain name is registered?
我注意到请求无效的 url requests.get(invalid_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)
通过捕获那些异常来判断域名是否注册是否准确?源码如下:
#!/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?
否;注册域并且没有根域名的 IP 地址是完全可以的,更不用说在该 IP 地址的端口 80 上有服务器 运行。
正如@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.
我注意到请求无效的 url requests.get(invalid_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)
通过捕获那些异常来判断域名是否注册是否准确?源码如下:
#!/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?
否;注册域并且没有根域名的 IP 地址是完全可以的,更不用说在该 IP 地址的端口 80 上有服务器 运行。
正如@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.