Benji Brown
Benji Brown

Reputation: 11

How do i send a non-recursive query to a dns server in dnspython/python in general?

I can't seem to figure out how to do this or if it is even doable, any help would be appreciated.

I have not tried much due to the lack of answers i can find on google.

Upvotes: 1

Views: 769

Answers (1)

Patrick Mevzek
Patrick Mevzek

Reputation: 12505

If not doing a recursive query it means you already know which nameservers to contact, and over UDP or TCP.

dnspython provides the query module to do exactly that, as explained in documentation at https://dnspython.readthedocs.io/en/stable/query.html#udp

Quick POC:

In [1]: import dns.message

In [2]: import dns.query

In [8]: msg = dns.message.make_query("www.stackoverflow.com", "A")

In [9]: print(msg)
id 18190
opcode QUERY
rcode NOERROR
flags RD
;QUESTION
www.stackoverflow.com. IN A
;ANSWER
;AUTHORITY
;ADDITIONAL

In [10]: res = dns.query.udp(msg, "205.251.196.9")

In [11]: print(res)
id 18190
opcode QUERY
rcode NOERROR
flags QR AA RD
;QUESTION
www.stackoverflow.com. IN A
;ANSWER
www.stackoverflow.com. 300 IN CNAME stackoverflow.com.
stackoverflow.com. 300 IN A 151.101.129.69
stackoverflow.com. 300 IN A 151.101.1.69
stackoverflow.com. 300 IN A 151.101.193.69
stackoverflow.com. 300 IN A 151.101.65.69
;AUTHORITY
stackoverflow.com. 172800 IN NS ns-1033.awsdns-01.org.
stackoverflow.com. 172800 IN NS ns-358.awsdns-44.com.
stackoverflow.com. 172800 IN NS ns-cloud-e1.googledomains.com.
stackoverflow.com. 172800 IN NS ns-cloud-e2.googledomains.com.
;ADDITIONAL

Why 205.251.196.9? Because this is the IP address of ns-1033.awsdns-01.org. which is one of the authoritative nameservers on stackoverflow.com.

Note how the answer has the flag AA which means authoritative.

Another way, especially if you want to profit for all the internal retry logic, handling truncation, switching to TCP, etc. is using dns.resolver.Resolver but then forcibly setting its nameservers attribute to the one and only nameserver you want to query, to bypass the usual default recursive use of this class (that will use by default the OS configured recursive nameservers).

Upvotes: 0

Related Questions