how to extract data from the response

182 views
Skip to first unread message

Nikos Skalis

unread,
Feb 14, 2023, 1:36:48 AM2/14/23
to dnspython-users
Hi,

I am using the dnspython lib for the first time, and I am struggling to find how I can extract an ip address and a port from the response to a dns query. More specifically,

import dns.name
import dns.message
import dns.query
import dns.flags
domain = "redis_exporter.service.nl-ams02c.consul."
msg = dns.message.make_query(dns.name.from_text(domain), dns.rdatatype.SRV)
resp = dns.query.udp(msg, "127.0.0.1", timeout=1, port=8600)
print(resp.answer)
# resp.answer[0].to_text()
# 'redis_exporter.service.nl-ams02c.consul. 0 IN SRV 1 1 9121 ac1f8def.addr.nl-ams02c.consul.'
# resp.answer[0].to_rdataset()
# <DNS IN SRV rdataset: [<1 1 9121 ac1f8def.addr.nl-ams02c.consul.>]>
print(resp.additional)
# resp.additional[0].to_text()
# 'ac1f8def.addr.nl-ams02c.consul. 0 IN A 172.31.x.x'

 In case of a SRV record I would like to extract the port number (9121) and from the additional section in case of a A record to extract the ip address (172.31.x.x).

How would you do that reliably without using text parsing (.to_text())?

Nikos

Bob Halley

unread,
Feb 14, 2023, 8:30:20 AM2/14/23
to dnspython-users
# sample program

import dns.message
import dns.name
import dns.query
import dns.rdataclass
import dns.rdatatype

qname = dns.name.from_text("something.example")
q = dns.message.make_query(qname, dns.rdatatype.SRV)
r = dns.query.udp(q, "127.0.0.1")

print(r)
print()

srrset = r.find_rrset(dns.message.ANSWER, qname, dns.rdataclass.IN, dns.rdatatype.SRV)
if srrset is not None:
    for srdata in srrset:
        print(f"the SRV port for {srdata.target} is {srdata.port}")
        # try to find the SRV A in the additional section
        arrset = r.find_rrset(
            dns.message.ADDITIONAL, srdata.target, dns.rdataclass.IN, dns.rdatatype.A
        )
        if arrset is not None:
            for rdata in arrset:
                print(f"the address of {srdata.target} is {rdata.address}")

# sample output

id 55226

opcode QUERY

rcode NOERROR

flags QR AA RD RA

;QUESTION

something.example. IN SRV

;ANSWER

something.example. 3600 IN SRV 1 1 9121 server.example.

;AUTHORITY

;ADDITIONAL

server.example. 3600 IN A 10.0.0.1

server.example. 3600 IN AAAA ::1


the SRV port for server.example. is 9121

the address of server.example. is 10.0.0.1

Nikos Skalis

unread,
Feb 16, 2023, 2:46:16 AM2/16/23
to Bob Halley, dnspython-users
Awesome. Thank you. 

On 14 Feb 2023, at 14:30, Bob Halley <dnsp...@gmail.com> wrote:


--
You received this message because you are subscribed to a topic in the Google Groups "dnspython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dnspython-users/lpP4oEnKy2o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dnspython-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dnspython-users/0f1a0a27-8aa6-4653-871f-17d9699cf2dbn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages