dns.py

https://pypi.org/project/dnslib/

1. Classes

The key DNS packet handling classes are in dnslib.dns and map to the standard DNS packet sections:

 . DNSRecord - container for DNS packet. Contains:
      * DNSHeader
      * Question section containing zero or more DNSQuestion objects
      * Authority section containing zero or more RR objects
      * Additional section containing zero or more RR objects
 . DNS RRs (resource records) contain an RR header and an RD object)
 . Specific RD types are implemented as subclasses of RD
 . DNS labels are represented by a DNSLabel class - in most cases this handles conversion to/from textual representation however does support arbitatry labels via a tuple of bytes objects

2. class DNSRecord(object)

To create a DNS Request Packet:
>>> d = DNSRecord.question("google.com")

2.1. classmethod

d = DNSRecord.parse(packet)

header = DNSHeader.parse(buffer)
questions = []
rr = []
auth = []
ar = []

@classmethod

def question(cls,qname,qtype="A",qclass="IN"):
    """
      Shortcut to create question
      >>> q = DNSRecord.question("www.google.com")
      >>> print(q)
            ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: ...
            ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
            ;; QUESTION SECTION:
            ;www.google.com.                IN      A

            >>> q = DNSRecord.question("www.google.com","NS")
            >>> print(q)
            ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: ...
            ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
            ;; QUESTION SECTION:
            ;www.google.com.                IN      NS
   """
   return DNSRecord(q=DNSQuestion(qname,getattr(QTYPE,qtype),                                     getattr(CLASS,qclass)))


q = DNSRecord.question("www.google.com","NS")


CategoryDns CategoryWatch CategoryTemplate

MoinQ: DNS/実装/python/dnslib/Classes (last edited 2021-03-03 20:38:36 by ToshinoriMaeno)