-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicit DNSError subclass for failed DNS lookups #3630
Comments
So this is somewhat reasonable, but made more opaque by the fact that urllib3 gets in the way here. urllib3 will basically always turn a connection setup problem like this one into a I think, as a first step, you should propose that urllib3 raise special exceptions when it gets problems from getaddrinfo. From there, Requests can introspect and look for such an exception, which gives us a better shot at raising an appropriate error. And at the very least, you will then be able to dig in to the exceptions to pull out the original (as all of those exceptions are just wrappers). |
Reported above. Do you have access to OS X machine to test what |
In 2.7.12:
|
The hack so far: import requests
def sitecheck(url):
status = None
message = ''
try:
resp = requests.head('http://' + url)
status = str(resp.status_code)
except requests.ConnectionError as exc:
# filtering DNS lookup error from other connection errors
# (until https://github.com/shazow/urllib3/issues/1003 is resolved)
if type(exc.message) != requests.packages.urllib3.exceptions.MaxRetryError:
raise
reason = exc.message.reason
if type(reason) != requests.packages.urllib3.exceptions.NewConnectionError:
raise
if type(reason.message) != str:
raise
if ("[Errno 11001] getaddrinfo failed" in reason.message or # Windows
"[Errno -2] Name or service not known" in reason.message or # Linux
"[Errno 8] nodename nor servname " in reason.message): # OS X
message = 'DNSLookupError'
else:
raise
return url, status, message
print sitecheck('wowsucherror')
print sitecheck('google.com')
|
I'm not sure what the status of this is a few years later... I found the above didn't work for me on current python, so came up with this. Note I'm only returning the OK status here.
|
In case someone is wondering what is the status as of now: urllib3 v2.0 will have a separate exception for this, see urllib3/urllib3@1831327 and urllib3/urllib3@8a1ac9f. |
In an effort to clean up the issue tracker to only have issues that are still relevant to the project we've done a quick pass and decided this issue may no longer be relevant for a variety of potential reasons, including:
If you think the issue should remain open, please comment so below or open a new issue and link back to the original issue. Again, thank you for opening the issue and for the discussion, it's much appreciated. |
There is no cross-platform way to catch DNS lookup errors with
requests
. At least I see that error message on Windows https://stackoverflow.com/questions/40145631/precisely-catch-dns-error-with-python-requests looks different from Linux in https://github.com/kennethreitz/requests/issues/3550Would be nice to get dedicated exception for this use case.
The text was updated successfully, but these errors were encountered: