Python's Dnspython Library usage guide

Source: Internet
Author: User
Tags server port

Installation method

wget http://www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz

TAR-ZXVF dnspython-1.9.4.tar.gz

CD dnspython-1.9.4

Python setup.py Install

Because usually in the test dudns or Jomodns when some operations manual completion inconvenient, so need to use the script, and in Python Dnspython This is used for the DNS operation of the library is very powerful, but helpless online most of the information only lists a few parts of the usage, So documenting what I normally use, I've basically been able to cope with most of the usage scenarios. Want to know Dnspython can log on to the official website to read the use of the document

Common tools

The most common use is to call the default resolver to send a resolution request, such as:

from DNS import Resolverans = resolver.query ( "www.baidu.com", < Span class= "hljs-string" > "A") print ( "QName:", Ans.qname) print ( " Reclass: ", Ans.rdclass) print ( "Rdtype:", Ans.rdtype) print ( "RRset:", Ans.rrset) print ( " Response: ", Ans.response)             

The result is:

(‘qname:‘, <DNS name www.baidu.com.>)(‘reclass:‘, 1)(‘rdtype:‘, 1)(‘rrset:‘, <DNS www.a.shifen.com. IN A RRset>)(‘response:‘, <DNS message, ID 64940>)

Here the resolution task is sent by default to the system default DNS server, in which the more important is response, in Dnspython Official document, response belongs to class Dns.message.Message, this class is also many DNS The return result of the query request, which is described in detail below.

The main member variables of the class are:

int flags   #The DNS flags of the message.int id #The query id; the default is a randomly chosen id.list of RRset addictionallist of RRset answerlist of RRset authority

Flags are the flags that return DNS messages (see the section on DNS in TCP/IP (Volume i)), which can be used to print the individual flags of a DNS message:

#!/bin/env Python2.7ans = Resolver.query ("Www.baidu.com","A")DefFlagcount(Flags, POS):if (flags/(2**pos))%2 = =1:ReturnTrueElseReturnFalseDefGetFlags15 aa_pos = 10 TC_pos = 9 rd_pos = 8 ra_pos = 7 QR_flag = Flagcount (Flags, qr_pos) Aa_flag = Flagcount (Flags, aa_pos) Tc_flag = Flagcount (Flags, tc_pos) Rd_flag = Flagcount (Flags, Rd_pos) Ra_flag = Flagcount (Flags, ra_pos) Flag_dic = { "QR": Qr_flag,  "AA": Aa_flag,  "TC": Tc_flag,  "RD": Rd_flag,  "RA": Ra_flag} print  "flag : ", for flag in flag_dic: if flag_ Dic[flag]: print flag,flags = ans.response.flagsGetFlags (Flags)    

The returned result is:

flag: AA RD QR RA

Another important class is rrset, which typically returns three section information using this class wrapper, often using the class function To_text () to make the parsing result appear as a string. Such as:

= resolver.query("www.baidu.com", "A")for i in ans.response.answer: print i.to_text()

The returned result is:

Www. Baidu. com. 1200InchCNAMEwww.a.com.www .a.shifen .com. 119 in A 220.181.112 .244www.a< Span class= "Hljs-selector-class" >.shifen.com. 119 in a 220.181< Span class= "Hljs-selector-class" >.111.188       
Send a request to a DNS server you've built

When you need to send a resolution request to your own DNS server, you can use the Dns.query class, which allows you to send a custom DNS resolution request to the specified address and port, following the description of the official documentation for several key member functions:

# #使用udp发送解析命令udp (q, where, Timeout=none, port=53, Af=none, Source=none, Source_port=0, ignore_unexpected=< Span class= "Hljs-keyword" >false, One_rr_per_rrset=false)  Return the response obtained after sending a query via UDP. return DNS. message. message object# #使用tcp发送解析命令tcp (Q, where, Timeout=none, port= 53, Af=none, Source=none, Source_port=0, one_rr_per_rrset=< Span class= "Hljs-keyword" >false) return the response obtained after sending a query via tcp.< Span class= "Hljs-keyword" >return DNS. message. message object            

Where the formal parameter where corresponds to the DNS server IP address, q corresponds to class dns.message.Message. The returned result is Dns.message.Message, which already describes how to use this class.
We can construct a parse request through Dns.message.make_query (). Take a look at the prototype of the Make_query () function:

make_query(qname, rdtype, rdclass=1, use_edns=None, want_dnssec=False, ednsflags=None, payload=None, request_payload=None, options=None)

Basically setting the first two values is enough. Therefore, sending a resolution request to your own DNS server can be briefly followed by the following steps:

import dnsSERVER = "1.1.1.1"#your DNS serverPORT = 53#DNS server port dns_query = dns.message.make_query("www.baidu.com", "A")response = dns.query.udp(dns_query, SERVER, port = PORT)for i in response.answer: print i.to_text()
Operate a DNS server of your own build

The

can also be dynamically updated with DNS through Dnspython. For example, the BIND server can use the RNDC tool to dynamically update bind, but it is not always convenient to operate the RNDC tool, and we can choose to use Dns.update to dynamically update bind.
A simple example: known as a zone tsig (primary and secondary synchronous encryption) Key-value {"Default": "werasdfasdfweffs==",}, we can use this transaction signature to update the zone. For example, I want to add 100 host records for zone testqa.com that are Rdata "1.1.1.1", respectively www1~www100, which can:

import dns.tsigkeyringimport dns.updateimport dns.queryZONE = "testqa.com"keyring = dns.tsigkeyring.from_text({‘default.any‘: ‘xxxxxxxxxxxxxxx‘})update_query = dns.update.Update(ZONE, keyring=keyring)for i in range(1,101): update_query.add("testqa" + str(i), 60, "1.1.1.1")

Python's Dnspython Library usage guide

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.