Dnspython is the DNS toolkit for Python. It supports almost all record types. It can be used for querying, zone transfers, and dynamic updates. It supports TSIG authentication messages and EDNS0.
Dnspython provides advanced and low-level access to DNS. the high-level class executes a query on the data for the given name, type, and class, and returns the answer set. the low-level class allows direct manipulation of DNS zones, messages, names, and records.
Official website: http://www.dnspython.org/
#! /env python3#coding=utf-8 "Module Domain name resolution method: Dnspthon provides a DNS parser class-resolver, using the query method to implement the domain Name query function method is defined as follows: query ( self,qname,rdtpye=1,rdclass=1,tcp=false,source=none,rasie_on_no_answer=true,source_port=0) where the QName parameter is the domain name of the query. The Rdtpye parameter specifies the RR resource type, which is commonly used to convert the hostname to an IP address, the MX record, the mail switch record, the domain name of the mail server, the CNAME record, the alias record, the mapping between the domain names, the NS record, the domain name server for the marked area, and the authorization subdomain. PTR record, direction resolution, reverse IP to host name, SOA record, SOA tag, a start authorization area definition Rdclass parameter is used to specify the network type, optional values are in,ch and HS, where in is the default, The most extensive TCP parameter is used to specify whether the query is enabled with the TCP protocol, and by default, false is not enabled. The source and Source_port parameters serve as the Origin address and port for the specified query, and the default is the query device IP address and the 0.rasie_on_on_answer=true parameter to specify whether to trigger an exception to the common parsing type-a record when the query is not answered. Implement a record Query method ' ' Import dns.resolverdomain = input (' please input an domain: ') #输入域名地址A = dns.resolver.query (domain, ' A ') # Specifies that the query type is a record for i in a.response.answer: #通过response. Answer method to get query response information for j in i.items: #遍历回应信息 print (j) ' Common parsing type-mx record ' ' Import dns.resolverdomain = input (' please input an domain: ') #指定域名为163 .commx = Dns.resolver.query (domain, ' MX ') #指定查询类型为MX记录for i in MX: #遍历回应结果, preference and exchanger information for output MX records print (' mx preference = ', i.preference, ' mail exchanger = ', i.exchange) ' Common parsing type-ns record "' Import dns.resolverdomain = input (' please input an domain: ') ) #只限输入一级域名, e.g. 163.comns = dns.resolver.query (domain, ' NS ') # Specifies that the query type is nsfor i in ns.response.answer: for j in i.items: print (J.to_text ()) ' Common parsing type-cname record ' Import dns.resolverdomain = input (' please input an domain: ') #指定查询类型为CNAME记录cname = dns.resolver.query (domain, ' CNAME ') #结果将回应cname后的目标信息for i in cname.response.answer: for j in i.items: print (J.to_text ())
Python Learning-dnspython