-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
windows DNS resolution: follow-up 9b0773c
related: #5176, #4421 prev was failing on systems where IPv6 is not available but DNS can resolve AAAA records (my artificial test environment with IPv6 disabled was also filtering AAAA DNS)
- Loading branch information
1 parent
1cfac92
commit c4fb58c
Showing
1 changed file
with
11 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -526,31 +526,36 @@ def needs_dns_resolving(host): | |
return False | ||
return True | ||
def resolve_with_dnspython(host): | ||
addrs = [] | ||
# try IPv6 | ||
try: | ||
answers = dns.resolver.query(host, dns.rdatatype.AAAA) | ||
return str(answers[0]) | ||
addrs += [str(answer) for answer in answers] | ||
except dns.exception.DNSException as e: | ||
pass | ||
except BaseException as e: | ||
print_error(f'dnspython failed to resolve dns (AAAA) with error: {e}') | ||
# try IPv4 | ||
try: | ||
answers = dns.resolver.query(host, dns.rdatatype.A) | ||
return str(answers[0]) | ||
addrs += [str(answer) for answer in answers] | ||
except dns.exception.DNSException as e: | ||
# dns failed for some reason, e.g. dns.resolver.NXDOMAIN | ||
# this is normal. Simply report back failure: | ||
raise socket.gaierror(11001, 'getaddrinfo failed') from e | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
SomberNight
Author
Member
|
||
except BaseException as e: | ||
# Possibly internal error in dnspython :( see #4483 | ||
print_error(f'dnspython failed to resolve dns (A) with error: {e}') | ||
if addrs: | ||
return addrs | ||
# Fall back to original socket.getaddrinfo to resolve dns. | ||
return host | ||
addr = host | ||
return [host] | ||
addrs = [host] | ||
if needs_dns_resolving(host): | ||
addr = resolve_with_dnspython(host) | ||
return socket._getaddrinfo(addr, *args, **kwargs) | ||
addrs = resolve_with_dnspython(host) | ||
list_of_list_of_socketinfos = [socket._getaddrinfo(addr, *args, **kwargs) for addr in addrs] | ||
list_of_socketinfos = [item for lst in list_of_list_of_socketinfos for item in lst] | ||
return list_of_socketinfos | ||
|
||
@log_exceptions | ||
async def set_parameters(self, net_params: NetworkParameters): | ||
|
1 comment
on commit c4fb58c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. 👍
Question: Would it make sense to make this conditional on
addrs
being empty here? Not sure if that can actually happen in reality (AAAA working, but A raising?) -- but if you did get an answer for AAAA but an exception for A: the question is -- is it better to return what you got or to propagate the exception outward?Honest question.