Advice about using dnspython

55 views
Skip to first unread message

Daniele Varrazzo

unread,
Aug 22, 2021, 11:27:13 AM8/22/21
to dnspython-users
Hello,

I am tying to use dsnpython in Psycopg 3 to implement two features:

- async lookup of hostnames
- SRV resolution (RFC 2782)

I am currently playing around the first feature, using the hostaddr libpq feature to associate addresses to host names [1]


However, the lack of /etc/hosts resolution gets in the way: I have found the need to specialise "localhost" because on Mac OS (and likely elsewhere) it is not resolved by the DNS [2], and I don't have so much DNS culture to understand if hardcoding it to 127.0.0.1 is a good idea. Even assuming that localhost is safe, there will be a discrepancy between sync and async host resolution (if manual resolution using dsnpython is only performed on async connections).


Have you got any suggestion about how to put together a more comprehensive async resolution of host names? Current code, if you would like to take a look, is available at [3].


Cheers

-- Daniele

Bob Halley

unread,
Aug 24, 2021, 1:22:05 PM8/24/21
to dnspython-users
dnspython is only a DNS tool, and it doesn't look in /etc/hosts or other sources of hostname data the way the system resolver would.  RFC 6761 makes it clear that localhost should always resolve to 127.0.0.1 for a v4 query, and ::1 for a v6 query.  We occasionally talk about adding /etc/hosts support but we've been trying to avoid it, because it would only solve part of the problem.  RFC  6761 also allows DNS software to hardwire the answer, so perhaps we should do that.  At any rate, if you do a special case you will be ok.

Your current code ([3]) only supports IPv4.  Unless you know that is enough, I'd recommend doing IPv6 AAAA queries also, e.g.:

ans = await async_resolver.resolve(host, 'A', raise_on_no_answer=False)
if ans.rrset is not None:
    # append to your hosts list here
ans = await async_resolver.resolve(host, 'AAAA', raise_on_no_answer=False)
if ans.rrset is not None:
    # append to your hosts list here

Reply all
Reply to author
Forward
0 new messages