我们如何从dnspython获取TXT,CNAME和SOA记录? [英] How do we get TXT, CNAME and SOA records from dnspython?

查看:563
本文介绍了我们如何从dnspython获取TXT,CNAME和SOA记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要具有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查询的示例:

TXT

http://www.dnspython.org/docs/1.15.0/dns.rdtypes.txtbase.TXTBase-class.html#section-InstanceVariables

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆