DNS toolkit for Python supporting almost all record types with high-level and low-level DNS operations
85
Comprehensive exception classes for handling DNS errors and operational failures. All dnspython exceptions inherit from DNSException, providing consistent error handling patterns across the library.
Core exception classes that form the foundation of dnspython's error handling system.
class DNSException(Exception):
"""
Abstract base class shared by all dnspython exceptions.
Supports both old/compatible mode (args only) and new/parametrized
mode (kwargs only) for consistent error reporting.
"""
class FormError(DNSException):
"""DNS message is malformed."""
class SyntaxError(DNSException):
"""Text input is malformed."""
class UnexpectedEnd(SyntaxError):
"""Text input ended unexpectedly."""
class TooBig(DNSException):
"""The DNS message is too big."""
class Timeout(DNSException):
"""The DNS operation timed out."""Specific exception classes for DNS resolution operations and query failures.
class NXDOMAIN(DNSException):
"""
The DNS query name does not exist.
Provides access to the canonical name through canonical_name() method
and supports both single and multiple query name scenarios.
"""
def canonical_name(self) -> str:
"""Get the canonical name if available from CNAME chains."""
class YXDOMAIN(DNSException):
"""The DNS query name exists when it should not."""
class NoAnswer(DNSException):
"""
The DNS response contains no answer to the question.
This can occur when a name exists but has no records of the
requested type, or when DNSSEC validation fails.
"""
class NoNameservers(DNSException):
"""No non-broken nameservers are available to answer the question."""
class NotAbsolute(DNSException):
"""Raised if an absolute DNS name is required but a relative name was provided."""
class NoRootSOA(DNSException):
"""Raised if for some reason there is no SOA RR at the DNS root name."""
class NoMetaqueries(DNSException):
"""Raised if a metaquery is attempted with a resolver that forbids them."""Exception classes specific to DNSSEC validation and cryptographic operations.
class ValidationFailure(DNSException):
"""
DNSSEC validation has failed.
Contains details about why validation failed and which keys
or signatures were involved in the failure.
"""
class UnsupportedAlgorithm(DNSException):
"""
A DNSSEC algorithm is not supported.
Raised when encountering DNSSEC keys or signatures using
cryptographic algorithms not supported by the current
configuration or available cryptographic libraries.
"""Exception classes for zone file operations and zone transfer failures.
class BadZone(DNSException):
"""The DNS zone is malformed."""
class NoSOA(BadZone):
"""The DNS zone has no SOA RR at its origin."""
class NoNS(BadZone):
"""The DNS zone has no NS RRset at its origin."""
class UnknownOrigin(DNSException):
"""The DNS zone's origin is unknown."""Exception classes for DNS message parsing and wire format operations.
class ShortHeader(FormError):
"""The DNS packet passed to from_wire() is too short to contain a header."""
class TrailingJunk(FormError):
"""The DNS packet passed to from_wire() has trailing junk."""
class UnknownHeaderField(BadHeader):
"""The header field name is unknown."""
class BadHeader(FormError):
"""The DNS message header is malformed."""import dns.resolver
import dns.exception
try:
result = dns.resolver.query('nonexistent.example.com', 'A')
except dns.resolver.NXDOMAIN as e:
print(f"Domain does not exist: {e}")
# Get canonical name if available from CNAME chain
try:
canonical = e.canonical_name()
print(f"Canonical name: {canonical}")
except:
print("No canonical name available")
except dns.resolver.NoAnswer as e:
print(f"No answer for query: {e}")
except dns.resolver.Timeout as e:
print(f"Query timed out: {e}")
except dns.exception.DNSException as e:
print(f"DNS error: {e}")import dns.query
import dns.exception
try:
response = dns.query.udp(query_message, '8.8.8.8', timeout=5)
except dns.exception.Timeout:
print("UDP query timed out, trying TCP")
try:
response = dns.query.tcp(query_message, '8.8.8.8', timeout=10)
except dns.exception.Timeout:
print("TCP query also timed out")
except dns.exception.FormError as e:
print(f"Malformed DNS message: {e}")
except dns.exception.TooBig as e:
print(f"Response message too large: {e}")import dns.zone
import dns.exception
try:
zone = dns.zone.from_file('example.com.zone', 'example.com.')
except dns.zone.NoSOA:
print("Zone file missing required SOA record")
except dns.zone.NoNS:
print("Zone file missing required NS records")
except dns.zone.BadZone as e:
print(f"Malformed zone file: {e}")
except dns.exception.SyntaxError as e:
print(f"Zone file syntax error: {e}")import dns.dnssec
import dns.exception
try:
dns.dnssec.validate(rrset, rrsigset, keys)
print("DNSSEC validation successful")
except dns.dnssec.ValidationFailure as e:
print(f"DNSSEC validation failed: {e}")
except dns.dnssec.UnsupportedAlgorithm as e:
print(f"Unsupported DNSSEC algorithm: {e}")DNSException (Exception)
├── FormError
│ ├── ShortHeader
│ ├── TrailingJunk
│ └── BadHeader
│ └── UnknownHeaderField
├── SyntaxError
│ └── UnexpectedEnd
├── TooBig
├── Timeout
├── NXDOMAIN
├── YXDOMAIN
├── NoAnswer
├── NoNameservers
├── NotAbsolute
├── NoRootSOA
├── NoMetaqueries
├── ValidationFailure
├── UnsupportedAlgorithm
├── BadZone
│ ├── NoSOA
│ └── NoNS
└── UnknownOriginclass DNSException(Exception):
"""Base exception class with support for parametrized error messages."""
msg: Optional[str]
kwargs: Dict[str, Any]
def __init__(self, *args, **kwargs): ...
def __str__(self) -> str: ...
class NXDOMAIN(DNSException):
"""Non-existent domain exception with canonical name support."""
def canonical_name(self) -> str:
"""Return canonical name from CNAME chain if available."""
class Timeout(DNSException):
"""Timeout exception with elapsed time information."""
def __init__(self, timeout: float = None): ...Install with Tessl CLI
npx tessl i tessl/pypi-dnspythondocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10