我们如何从 dnspython 获取 TXT、CNAME 和 SOA 记录? [英] How do we get TXT, CNAME and SOA records from dnspython?
问题描述
我需要一个 dns 查询功能来查询服务器的各种记录.我想出了如何获取 MX 记录(大多数示例都展示了这一点)、A 记录和 NS 记录.如何获取 TXT、CNAME 和 SOA 记录?
I have a requirement to have a dns query function to query a server for various records. I figured out how to get the MX record (most of the examples show this), A record and NS record. How do I get the TXT, CNAME and SOA records?
示例代码片段:
import dns.resolver
answer=dns.resolver.query("google.com", "A")
for data in answer:
print data.address
我尝试用 TXT 替换查询类型,用 data.text、data.data 等替换 data.address 对象,但最终出现属性错误.我前面提到的数据类型的引用是什么?
I tried replacing the query type with TXT and the data.address object with data.text, data.data etc, but ended up with attribute errors. What are the references for the data types I mentioned earlier?
推荐答案
(回答如何计算返回的数据)
(To answer how you can figure out the returned data)
您可以通过类似的方式获取 TXT、CNAME 和 SOA 记录,但您只需要根据 DNS 响应对象获取正确的属性即可.
You can get the TXT, CNAME, and SOA records a similar way but you just have to get the correct attributes depending on the DNS response object.
使用内置的 python dir() 是您的朋友,也是找出 DNS 响应对象中存在哪些属性的一种方法 - 在 API 文档不可用时很方便.
Using the python dir() built-in is your friend and one way to figure out what attributes exist in the DNS response object - handy when API documentation is not available.
要找出适当的属性,请将您的 for 循环暂时更改为以下内容:
To figure out the appropriate attributes, change your for loop temporarily to the following:
for data in answer:
print dir(data)
print data
另一种更快的方法是查看 dnspython 的 API 文档,这些页面列出了每个返回对象的属性.
Another and quicker way is to look at the API documentation for dnspython, these pages list the attributes for each returned object.
最后,如果库在 python 中,您可以查看源代码,如果没有,则可以查看 C 代码是否可用.
Lastly, you could look at the source if the library is in python or if not, then if the C code is available.
(并回答您的问题:)
以下是 TXT、CNAME 和 SOA 查询的示例:
文本
answers = dns.resolver.query('google.com', 'TXT')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
for txt_string in rdata.strings:
print ' TXT:', txt_string
CNAME
http://www.dnspython.org/docs/1.15.0/dns.rdtypes.ANY.CNAME.CNAME-class.html
answers = dns.resolver.query('mail.google.com', 'CNAME')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' cname target address:', rdata.target
SOA
http://www.dnspython.org/docs/1.15.0/dns.rdtypes.ANY.SOA.SOA-class.html#section-InstanceVariables
answers = dns.resolver.query('google.com', 'SOA')
print 'query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' serial: %s tech: %s' % (rdata.serial, rdata.rname)
print ' refresh: %s retry: %s' % (rdata.refresh, rdata.retry)
print ' expire: %s minimum: %s' % (rdata.expire, rdata.minimum)
print ' mname: %s' % (rdata.mname)
这篇关于我们如何从 dnspython 获取 TXT、CNAME 和 SOA 记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!