Precisely Catch DNS Error With Python Requests
I am trying to make a check for expired domain name with python-requests. import requests try: status = requests.head('http://wowsucherror') except requests.ConnectionError a
Solution 1:
Done this with this hack, but please monitor https://github.com/psf/requests/issues/3630 for a proper way to appear.
# for Python 2 compatibility
from __future__ import print_function
import requests
def sitecheck(url):
status = None
message = ''
try:
resp = requests.head('http://' + url)
status = str(resp.status_code)
if ("[Errno 11001] getaddrinfo failed" in str(exc) or # Windows
"[Errno -2] Name or service not known" in str(exc) or # Linux
"[Errno 8] nodename nor servname " in str(exc)): # OS X
message = 'DNSLookupError'
else:
raise
return url, status, message
print(sitecheck('wowsucherror'))
print(sitecheck('google.com'))
Solution 2:
You could use lower-level network interface, socket.getaddrinfo https://docs.python.org/3/library/socket.html#socket.getaddrinfo
import socket
def dns_lookup(host):
try:
socket.getaddrinfo(host, 80)
except socket.gaierror:
return False
return True
print(dns_lookup('wowsucherror'))
print(dns_lookup('google.com'))
Solution 3:
I have a function based on the earlier answer above which seems to no longer work. This function checks "liveness" of a url based on resolution and also the requests .ok function rather than just specific to resolution errors but can be adapted easily to suit.
def check_live(url):
try:
r = requests.get(url)
live = r.ok
except requests.ConnectionError as e:
if 'MaxRetryError' not in str(e.args) or 'NewConnectionError' not in str(e.args):
raise
if "[Errno 8]" in str(e) or "[Errno 11001]" in str(e) or ["Errno -2"] in str(e):
print('DNSLookupError')
live = False
else:
raise
except:
raise
return live
Post a Comment for "Precisely Catch DNS Error With Python Requests"