4

I am learning how to use the python dns object. Quick question as I see many examples show methods of using the dns.resolver method with the DNS record type(CNAME, NS, etc). Is there a way to use this dns object to query a DNS name and pull it's resolution with the record type. Similar to what DIG supplies in the answer section.

Thanks,

Jim

4

5 回答 5

5

你可以得到类型rdatatype

>>> import dns.resolver
>>> answer = dns.resolver.query('google.com')
>>> rdt = dns.rdatatype.to_text(answer.rdtype)
>>> print(rdt)
A
于 2017-07-11T22:41:01.777 回答
3

下面是一个 CNAME 示例:

>>> cname = dns.resolver.query("mail.unixy.net", 'CNAME')
>>> for i in cname.response.answer:
...     for j in i.items:
...             print j.to_text()
... 
unixy.net.

TXT:

>>> txt = dns.resolver.query("unixy.net", 'TXT')
>>> for i in txt.response.answer:
...     for j in i.items:
...             print j.to_text()
... 
"v=spf1 ip4:..."

NS:

>>> ns = dns.resolver.query("unixy.net", 'NS')
>>> for i in ns.response.answer:
...     for j in i.items:
...             print j.to_text()
... 
ns2.unixy.net.
ns1.unixy.net.

您可以按照相同的模式获取大多数记录。多个响应查询存储在列表中。所以循环有时是必要的(例如:multiple A 和 NS recs)。

于 2013-10-05T05:59:32.793 回答
2

到目前为止,我发现确定它是 A 还是 CNAME 答案的唯一检查是测试 qname 属性是否等于 canonical_name 属性。

answer = dns.resolver.query('www.example.com')
if answer.qname == answer.canonical_name:
    print "This is A record"
else:
    print "This isn't A, probably CNAME..."
于 2014-01-28T16:05:24.010 回答
1

这个怎么样?

In [1]: import dns.resolver

In [2]: dns.resolver.query('chipy.org').__dict__
Out[2]: 
{'canonical_name': <DNS name chipy.org.>,
 'expiration': 1304632023.2383349,
 'qname': <DNS name chipy.org.>,
 'rdclass': 1,
 'rdtype': 1,
 'response': <DNS message, ID 64039>,
 'rrset': <DNS chipy.org. IN A RRset>}
于 2011-05-05T20:49:58.863 回答
0

看起来您需要推出自己的 Resolver 类。调用 dns.resolver.query 返回的 Answer 对象仅包含与请求特别匹配的记录,默认情况下恰好是 A 记录。一切都在那里,一路上迷路了。如果您打印响应,您可以明白我的意思。

#!/usr/bin/env python

import dns.resolver

answer = dns.resolver.query('www.clarkmania.com')
print answer.response
print "------"
print answer.rrset
于 2011-05-05T20:40:47.677 回答