Getting data from response - novice here

114 views
Skip to first unread message

ble...@gmail.com

unread,
Mar 14, 2019, 11:56:53 AM3/14/19
to dnspython-users
I am having trouble understanding how to get what I need from a query of a PTR record. I have no idea how to interpret the documentation and while I am sure it is very comprehensive, it makes frustratingly little sense to someone who has been programming for only a couple of months. What I am doing is generating a query for a PTR:

ans = query('10.67.168.192.in-addr.arpa', 'PTR')

It works and I am able to display the answer with a for loop:

>>> for rdata in ans:
...     print(rdata)
...
host2
.foo.com.

But when I attempt to run a comparison on that data, there is no match:

>>> current = 'host2.foo.com.'
>>> for rdata in ans:
...     if rdata == current:
...             print('Same')
...
>>>

So, what is the difference between the print function and the operation in terms of how the data is processed? How can I compare the RR in the answer to the data in the variable? 

Bob Halley

unread,
Mar 14, 2019, 12:16:45 PM3/14/19
to dnspython-users
When you iterate the answer object, you're actually iterating the RRset that is the answer to the question you asked, so you're getting a sequence of Rdatas of whatever type you asked for.  In your case, it's a sequence of PTR objects.  So, you're comparing a PTR object to a string, and those are not ever going to be equal.  The name the PTR points to is in the "target" field of the PTR object, and that field is a dns.name.Name, not a string, so you want to compare that field to a name.  Here's how to do that:

```import dns.resolver
import dns.name

google_dns = dns.name.from_text('google-public-dns-a.google.com')
answer = dns.resolver.query('8.8.8.8.in-addr.arpa', 'PTR')
for rdata in answer:
    if rdata.target == google_dns:
        print('Found', google_dns)
```

Bob Halley

unread,
Mar 14, 2019, 12:18:28 PM3/14/19
to dnspython-users
ignore the triple back quotes in my reply, I failed at blockquoting the source :)

ble...@gmail.com

unread,
Mar 14, 2019, 1:23:55 PM3/14/19
to dnspython-users
Thanks so much for the quick response. I was thinking that it had to do with how the data is stored in the class, but I'm not schooled enough to figure out how to pick that information out of the docs. Much appreciated, sir, you have saved me a lot of frustration.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages