dnspython Documentation

repository·main·Indexed 25 days ago

https://github.com/rthalley/dnspython

A comprehensive DNS toolkit for Python (version 2.9.0dev0) that supports almost all record types. It provides high-level classes for easy queries and low-level classes for direct manipulation of DNS zones, messages, names, and records. Features include asynchronous support for asyncio and Trio via dns.asyncquery and dns.asyncresolver, DNSSEC functionality in dns.dnssec, and support for DNS-over-HTTPS (DoH) and DNS-over-QUIC (DoQ).

Tokens
12.5K
Snippets
12
Records
143
Agent score
80%

What's inside dnspython

  1. Overview of dnspython capabilities

    main

    dnspython is a DNS toolkit for Python designed for tasks such as DNS queries, zone transfers, dynamic updates, and nameserver testing. It provides two levels of access:

    1. High-level access: Classes that perform queries for specific data (name, type, and class) and return answer sets.
    2. Low-level access: Classes that allow direct manipulation of DNS zones, messages, names, and records. Most Resource Record (RR) types are supported.
  2. Understand dnspython asynchronous backends

    main

    dnspython uses "backends" to implement library-specific asynchronous functionality for asyncio and Trio. This allows the generic asynchronous DNS code to run on different event loops.

    To determine which backend is currently in use, dnspython follows this logic:

    1. If the sniffio module is installed, dnspython uses it to "sniff" the active event loop.
    2. If sniffio is not available, dnspython attempts to detect asyncio directly.
  3. Use dns.asyncquery for low-level DNS message exchange

    main

    The dns.asyncquery module is used for sending DNS messages to servers and processing their responses.

    Note: If you require high-level "stub resolver" behavior (resolving hostnames to IP addresses), use the dns.asyncresolver module instead of dns.asyncquery.

  4. Use the dns.asyncresolver module for asynchronous stub resolution

    main
    The dns.asyncresolver module provides an asynchronous implementation of a 'stub resolver'. This allows for performing DNS queries using Python's asyncio patterns, enabling non-blocking network operations during name resolution.
  5. Represent and manipulate DNS messages with dns.message

    main
    The dns.message module provides the dns.message.Message class and its subclasses to represent single DNS messages as defined by RFC 1035 and its extensions. You can use this module to construct, manipulate, and manage DNS messages, including support for TSIG signatures and EDNS. Messages can be serialized to a textual form and read back from that form.
  6. Install dnspython from source

    main

    If pip is unavailable, you can install from a downloaded zip file or by cloning the GitHub repository. After unzipping or cloning, run setup.py install using the appropriate command for your operating system.

    Note: The master branch on GitHub is under active development and may be unstable.

  7. Enable caching in the dnspython resolver

    main
    By default, the dns.resolver does not cache responses. To enable caching, you must create a cache instance and assign it to the resolver's cache attribute. Once configured, the resolver will cache both positive and negative responses, respecting the DNS TTL (Time To Live) of the data to ensure expired entries are not returned.