DNS toolkit for Python supporting almost all record types with high-level and low-level DNS operations
85
Utility modules providing helper functions for address processing, reverse DNS operations, internationalization, and common DNS operations. These modules support the core DNS functionality with specialized tools for working with IP addresses, domain names, and text processing.
Convert between IP addresses and their corresponding reverse DNS names for PTR record lookups.
def from_address(text: str) -> dns.name.Name:
"""
Convert an IPv4 or IPv6 address to reverse-map domain name.
Args:
text (str): IPv4 or IPv6 address in textual form (e.g. '127.0.0.1', '::1')
Returns:
dns.name.Name: Reverse-map domain name (e.g. 1.0.0.127.in-addr.arpa.)
Raises:
dns.exception.SyntaxError: If address is malformed
"""
def to_address(name: dns.name.Name) -> str:
"""
Convert reverse-map domain name back to IP address.
Args:
name (dns.name.Name): Reverse DNS name
Returns:
str: IP address in textual form
Raises:
dns.exception.SyntaxError: If name is not a valid reverse name
"""
# Domain constants
ipv4_reverse_domain: dns.name.Name # in-addr.arpa.
ipv6_reverse_domain: dns.name.Name # ip6.arpa.Low-level address conversion and validation functions for IPv4 and IPv6 addresses.
def inet_pton(family: int, text: str) -> bytes:
"""
Convert text representation of address to binary form.
Args:
family (int): Address family (socket.AF_INET or socket.AF_INET6)
text (str): Address in text form
Returns:
bytes: Binary representation of address
"""
def inet_ntop(family: int, address: bytes) -> str:
"""
Convert binary address representation to text form.
Args:
family (int): Address family (socket.AF_INET or socket.AF_INET6)
address (bytes): Binary address representation
Returns:
str: Address in text form
"""
def af_for_address(text: str) -> int:
"""
Determine address family for given address text.
Args:
text (str): IP address in text form
Returns:
int: socket.AF_INET for IPv4, socket.AF_INET6 for IPv6
Raises:
ValueError: If address format is invalid
"""
def is_multicast(text: str) -> bool:
"""
Check if address is a multicast address.
Args:
text (str): IP address in text form
Returns:
bool: True if address is multicast
"""Convert between E.164 telephone numbers and their ENUM domain representations.
def from_e164(text: str, origin: dns.name.Name = dns.name.root) -> dns.name.Name:
"""
Convert E.164 number to ENUM domain name.
Args:
text (str): E.164 number (e.g. '+1 555 1212')
origin (dns.name.Name): ENUM domain origin (default: e164.arpa.)
Returns:
dns.name.Name: ENUM domain name (e.g. 2.1.2.1.5.5.5.1.e164.arpa.)
"""
def to_e164(name: dns.name.Name, origin: dns.name.Name = dns.name.root,
want_plus_prefix: bool = True) -> str:
"""
Convert ENUM domain name to E.164 number.
Args:
name (dns.name.Name): ENUM domain name
origin (dns.name.Name): ENUM domain origin
want_plus_prefix (bool): Include '+' prefix in result
Returns:
str: E.164 number (e.g. '+15551212')
"""Specialized functions for IPv4 address validation and manipulation.
def inet_aton(text: str) -> bytes:
"""
Convert IPv4 address from text to 32-bit binary format.
Args:
text (str): IPv4 address in dotted decimal notation
Returns:
bytes: 4-byte binary representation
Raises:
dns.exception.SyntaxError: If address is invalid
"""
def inet_ntoa(address: bytes) -> str:
"""
Convert IPv4 address from binary to text format.
Args:
address (bytes): 4-byte binary representation
Returns:
str: IPv4 address in dotted decimal notation
"""Specialized functions for IPv6 address validation, manipulation, and format conversion.
def inet_aton(text: str) -> bytes:
"""
Convert IPv6 address from text to 128-bit binary format.
Args:
text (str): IPv6 address in standard notation
Returns:
bytes: 16-byte binary representation
Raises:
dns.exception.SyntaxError: If address is invalid
"""
def inet_ntoa(address: bytes) -> str:
"""
Convert IPv6 address from binary to text format.
Args:
address (bytes): 16-byte binary representation
Returns:
str: IPv6 address in standard notation
"""
def is_mapped(address: bytes) -> bool:
"""
Check if IPv6 address is IPv4-mapped.
Args:
address (bytes): 16-byte IPv6 address
Returns:
bool: True if address is IPv4-mapped (::ffff:x.x.x.x)
"""Parse and validate DNS TTL (Time To Live) values from text representations.
def from_text(text: str) -> int:
"""
Convert TTL from text to seconds.
Supports BIND-style suffixes: s (seconds), m (minutes),
h (hours), d (days), w (weeks).
Args:
text (str): TTL in text form (e.g. '1h', '300', '1d2h')
Returns:
int: TTL in seconds
Raises:
dns.exception.SyntaxError: If TTL format is invalid
"""
MAX_TTL: int # Maximum allowed TTL value (2^31 - 1)Handle Extended DNS (EDNS) options for modern DNS features and extensions.
def get_option_class(otype: int) -> type:
"""
Get the option class for the specified option type.
Args:
otype (int): EDNS option type code
Returns:
type: Option class for handling this option type
"""
def option_from_wire(otype: int, wire: bytes, current: int,
olen: int) -> 'Option':
"""
Create an option from wire format data.
Args:
otype (int): Option type
wire (bytes): Wire format data
current (int): Current position in wire data
olen (int): Option length
Returns:
Option: Parsed EDNS option
"""
class Option:
"""Base class for EDNS options."""
def __init__(self, otype: int): ...
def to_wire(self, file: Any = None) -> bytes: ...
def from_wire(cls, otype: int, wire: bytes, current: int, olen: int): ...import dns.reversename
# Convert IP addresses to reverse DNS names
ipv4_reverse = dns.reversename.from_address('192.168.1.1')
print(ipv4_reverse) # 1.1.168.192.in-addr.arpa.
ipv6_reverse = dns.reversename.from_address('2001:db8::1')
print(ipv6_reverse) # 1.0.0.0...0.8.b.d.0.1.0.0.2.ip6.arpa.
# Convert reverse names back to addresses
address = dns.reversename.to_address(ipv4_reverse)
print(address) # 192.168.1.1import dns.inet
import socket
# Determine address family
family = dns.inet.af_for_address('192.168.1.1') # socket.AF_INET
family = dns.inet.af_for_address('::1') # socket.AF_INET6
# Check for multicast addresses
is_multi = dns.inet.is_multicast('224.0.0.1') # True (IPv4 multicast)
is_multi = dns.inet.is_multicast('ff02::1') # True (IPv6 multicast)
# Convert between binary and text
binary = dns.inet.inet_pton(socket.AF_INET, '192.168.1.1')
text = dns.inet.inet_ntop(socket.AF_INET, binary)import dns.e164
# Convert phone number to ENUM domain
enum_name = dns.e164.from_e164('+1 555 1212')
print(enum_name) # 2.1.2.1.5.5.5.1.e164.arpa.
# Convert ENUM domain back to phone number
phone = dns.e164.to_e164(enum_name)
print(phone) # +15551212import dns.ttl
# Parse TTL values with units
ttl_seconds = dns.ttl.from_text('1h') # 3600
ttl_seconds = dns.ttl.from_text('30m') # 1800
ttl_seconds = dns.ttl.from_text('1d2h') # 93600
ttl_seconds = dns.ttl.from_text('300') # 300
# Maximum TTL validation
print(dns.ttl.MAX_TTL) # 2147483647# Reverse name constants
ipv4_reverse_domain: dns.name.Name = dns.name.from_text('in-addr.arpa.')
ipv6_reverse_domain: dns.name.Name = dns.name.from_text('ip6.arpa.')
# TTL limits
MAX_TTL: int = 2147483647
# EDNS option base class
class Option:
"""Base class for all EDNS options."""
otype: int
def __init__(self, otype: int): ...
def to_wire(self, file: Any = None) -> bytes: ...
def from_wire(cls, otype: int, wire: bytes, current: int, olen: int): ...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