dnspython is a DNS toolkit implemented in python that supports record types, queries, transferring and dynamically updating ZONE information, and more. It is said that dnspython can replace tools such as dig, nslookup, and so on. Here we will introduce the use of dnspython module.
dnspython source code installation
Here is an introduction to the installation of the dnspython module, using the source code of the installation, currently using the version 1.16.0
Installation as follows:
wget /kits/1.16.0/dnspython-1.16.
tar -zxvf dnspython-1.16.
cd dnspython-1.16.0 python install
Seeing the message in the red box indicates that the installation is complete.
Okay, on to the main topic of the day, python modules.
python module domain name analysis method explained
Normally test DNS, some of the operations are not convenient to deal with manually, you need to write a script to achieve this is the idea of using the dnspython module, which provides a large number of DNS processing methods, we know that the most commonly used method is the domain name query, dnspyhton library provides a parser class (resolver), we can use it query method to achieve the following domain name query function. Here to say query:
query(self, qname, rdtype = 1, rdclass = 1, tcp = False, source = None, raise_on_no_answer = True, source_port = 0)
qname: the domain name of the query.
rdtype :Specify the type of RR resource
All rdtype types are:
The following are commonly used.
A record: convert hostname to IP address
MX records: mail exchange records, defining the domain name of the mail server
CNAME records: alias records that enable mapping between domain names
NS Record: Domain Name Servers and Authorized Subdomains for Tagged Regions
PTR record: reverse resolution, as opposed to an A record, which translates an IP address into a host name
SOA record: SOA tag, definition of a starting authorization zone
rdclass: specifies the network type, the optional values are CH, IN. where IN is the default and most widely used.
tcp : Specifies whether to start the TCP protocol for the query, the default is not.
source and source_port: the specified query source address and port.
raise_on_no_answer : Specifies whether to raise an exception if the query is not answered, default is True.
Below we start to explain the common parsing types.
Explanation of parsing types
A records
#!/usr/local/bin/python38 # -*- coding: utf-8 -*- import domain = input('Please input an domain: ') #Enter the domain address A = (domain, 'A') # Query records for A records for i in : for j in : if == 1: #add judgement,Otherwise, there will beAttributeError: 'CNAME' object has no attribute 'address' print()
Run the code to see the results, here is an example of a domain name:
This way we will be the domain name is resolved. Let's take a look at the effect:
MZ Records
#!/usr/local/bin/python38 # -*- coding: utf-8 -*- import domain = input('Input an domain : ') MX = (domain, 'MX') # Specify that the parsing type is an MX record for i in MX: # Traversing response results print('MX preference =', , 'mail exchanger =', )
Run the code to see the results, here is an example of a domain name:
We can see the preference value (priority) and exchange value (exchange address), where the priority is 10 by default, and MX records can realize the primary and secondary server settings by setting the priority, and the smaller the number in the "Priority" means the higher the level, and the "Priority" is only valid for MX records. Priority" is only valid for MX records.
NS Records
NS (Name Server) domain name server record. Used to indicate which server resolves the domain name. When registering a domain name, there are always default DNS servers, and each registered domain name is resolved by a DNS name server.
The following implements the NS record query method:
#!/usr/local/bin/python38 import domain = input('Input an domain : ') NS = (domain, 'NS') for i in : for j in : print(j.to_text())
The results of the run are as follows:
Only input first level domain name, such as. If you enter a second or multiple level domain name, such as :, it is an error, we can try it to verify the problem:
CNAME records
When using CNAME, only the hostname can be used for the destination host address of the CNAME, not the IP address. The hostname cannot be preceded by any other prefix, such as http://etc. are not allowed.
Take a look at the following code to implement the CNAME record method:
#!/usr/local/bin/python38 import domain = input('Input an domain: ') CNAME = (domain,'CNAME') for i in : for j in : print(j.to_text())
The results of the run are as follows:
The result is a domain name, which is the alias pointing to the domain name. Equivalent to the use of sub-domains to replace the ip address, the advantage is that if the ip address changes, you only need to change the sub-domain name resolution, and do not need to change the ip address resolution one by one.
DNS domain name polling service monitoring
Most of the DNS resolver is a domain name corresponds to an IP address, but through the DNS polling technology can do a domain name corresponds to more than one IP, so as to realize the most simple and efficient load balancing, but the biggest disadvantage of this party is that the target host can not be automatically rejected when unavailable, so it is important to do a good job of monitoring the availability of the service of the business host.
Let's implement the domain name resolution and get a list of all A-record resolution IPs for the domain name.
#!/usr/local/bin/python38 import import httplib2 iplist = [] appdomain = "" def get_iplist(domain=""): try: A = (domain,"A") except Exception as e: print("dns resolver error:" + str(e)) return for i in : for j in : if == 1: () return True def checkip(ip): checkurl = 'http://' + ip + ":80" getcontent = "" (5) conn = () try: resp,getcontent=(checkurl) finally: if resp['status']== "200": print(ip+"[OK]") else: print(ip+"[Error]") if __name__ == "__main__": if get_iplist(appdomain) and len(iplist) > 0: for ip in iplist: checkip(ip)
The results of the run are as follows:
As you can see from the result, the domain name resolves to 3 IP addresses, and the services are all normal. In the above code, the first step is to get the business domain name A record information through the () method, query the list of all IP addresses, and then use the request() method of the httplib module to request the monitoring page by GET to monitor the IP of all services of the business to see if the service is normal.
summarize
We know that the conversion of an IP address into a readable format or word is called a domain name. In python, domain name to IP address conversion is managed by the python module dnspython. It supports methods to record types, query, transfer and dynamically update ZONE information. For those interested, you can check out the dnspython source code.
To this point this article on the Python module domain name dnspython analysis of the article is introduced to this, more related Python module domain name dnspython content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!