Python DNS Processing Module Dnspython

Source: Internet
Author: User

Python DNS Processing Module Dnspython

The following is a list of common methods used in the Dnspython module, drawing on the book, "Python Automated operations: Technology and best Practices"

Dnspython provides a large number of DNS processing methods, the most commonly used is the domain name query. Dnspython provides a DNS parsing class resolver, which uses its query method to implement the querying function of the domain name.
Query (self, QName, Rdtype = 1, Rdclass = 1, TCP = False, Source = None, Raise_on_no_answer = True, Source_port = 0)
The QName parameter is the domain name of the query, and the Rdtype parameter is used to specify the RR resource.
A record: Convert host name to IP address
MX record: Mail exchange record that defines the domain name of the mail server
CNAME records: Alias records for mapping between domain names
NS records: Domain name servers and authorization subdomains for labeled zones
PTR record: Reverse parsing, in contrast to a record, converts IP address to host name
SOA record: SOA tag, definition of a starting authorization area
Rdclass specified network type, IN,CH,HS, in default
TCP Specifies whether the query enables the TCP protocol (not enabled by default)
Source and Source_port Specify the address and port of the query source, which defaults to the IP address of the query device and 0
RAISE_ON_NO_ANSWER Specifies whether to trigger an exception when the query does not answer, by default True

Here is the code

#! /usr/bin/env python
#--Coding:utf-8-*-**
Import Dns.resolver

Domain = raw_input (' Please input a domain: ')
Class DNS:
def get_a (self,domain): # query A record
Print (' Output a record ')
Try
A = Dns.resolver.query (Domain, ' A ')
For I in A.response.answer:
For J in I.items:
Print (j.address)
Except Exception as E:
Print (' Not queried to a record ')

def get_mx (Self,domain): # Querying MX Records
Print (' Output MX record ')
Try
mx = dns.resolver.query (Domain, ' MX ')
For I in MX:
print ' MX preference = ', I.preference, ' Mail exchanger= ', I.exchange
Except Exception as E:
Print (' not queried to MX record ')

def Get_ns (Self,domain): # Querying NS Records
Print (' Output NS record ')
Try
NS = Dns.resolver.query (Domain, ' NS ')
For I in Ns.response.answer:
For J in I.items:
Print J.to_text ()
Except Exception as E:
Print (' not queried to NS record ')

def get_cname (Self,domain): # Querying CNAME Records
Print (' Output CNAME record ')
Try
CNAME = Dns.resolver.query (Domain, ' cname ')
For I in Cname.response.answer:
For J in I.items:
Print J.to_text ()
Except Exception as E:
Print (' Not queried to CNAME ')

If name = = 'main':
D = DNS ()
D.get_a (Domain)
Print ('----------------')
D.GET_MX (Domain)
Print ('----------------')
D.get_ns (Domain)
Print ('----------------')
D.get_cname (Domain)
Print ('----------------')

Python DNS Processing Module Dnspython

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.