Miscellaneous Utilities

Generic Internet address helper functions.

dns.inet.af_for_address(text: str) int[source]

Determine the address family of a textual-form network address.

Parameters:

text (str) – The textual address.

Raises:

ValueError – If the address family cannot be determined.

Return type:

int

dns.inet.any_for_af(af)[source]

Return the ‘any’ address for the specified address family.

dns.inet.canonicalize(text: str) str[source]

Verify that address is a valid text form IPv4 or IPv6 address and return its canonical text form. IPv6 addresses with scopes are rejected.

Parameters:

text (str) – The address in textual form.

Raises:

ValueError – If the text is not a valid address.

dns.inet.inet_ntop(family: int, address: bytes) str[source]

Convert the binary form of a network address into its textual form.

Parameters:
  • family (int) – The address family.

  • address (bytes) – The network address in binary form.

Raises:

NotImplementedError – If the address family is not implemented.

Return type:

str

dns.inet.inet_pton(family: int, text: str) bytes[source]

Convert the textual form of a network address into its binary form.

Parameters:
  • family (int) – The address family.

  • text (str) – The textual address.

Raises:

NotImplementedError – If the address family is not implemented.

Return type:

bytes

dns.inet.is_address(text: str) bool[source]

Is the specified string an IPv4 or IPv6 address?

Parameters:

text (str) – The textual address.

Return type:

bool

dns.inet.is_multicast(text: str) bool[source]

Is the textual-form network address a multicast address?

Parameters:

text (str) – The textual address.

Raises:

ValueError – If the address family cannot be determined.

Return type:

bool

dns.inet.low_level_address_tuple(high_tuple: tuple[str, int], af: int | None = None) Any[source]

Given a “high-level” address tuple, i.e. an (address, port) return the appropriate “low-level” address tuple suitable for use in socket calls.

If an af other than None is provided, it is assumed the address in the high-level tuple is valid and has that af. If af is None, then af_for_address will be called.

IPv4 helper functions.

dns.ipv4.canonicalize(text: str | bytes) str[source]

Verify that address is a valid text form IPv4 address and return its canonical text form.

Parameters:

text (str or bytes) – The IPv4 address in textual form.

Raises:

dns.exception.SyntaxError – If the text is not valid.

dns.ipv4.inet_aton(text: str | bytes) bytes[source]

Convert an IPv4 address in text form to binary form.

Parameters:

text (str or bytes) – The IPv4 address in textual form.

Return type:

bytes

dns.ipv4.inet_ntoa(address: bytes) str[source]

Convert an IPv4 address in binary form to text form.

Parameters:

address (bytes) – The IPv4 address in binary form.

Return type:

str

IPv6 helper functions.

dns.ipv6.canonicalize(text: str | bytes) str[source]

Verify that address is a valid text form IPv6 address and return its canonical text form. Addresses with scopes are rejected.

Parameters:

text (str or bytes) – The IPv6 address in textual form.

Raises:

dns.exception.SyntaxError – If the text is not valid.

dns.ipv6.inet_aton(text: str | bytes, ignore_scope: bool = False) bytes[source]

Convert an IPv6 address in text form to binary form.

Parameters:
  • text (str or bytes) – The IPv6 address in textual form.

  • ignore_scope (bool) – If True, a scope is ignored; if False (the default), a scope is an error.

Return type:

bytes

dns.ipv6.inet_ntoa(address: bytes) str[source]

Convert an IPv6 address in binary form to text form.

Parameters:

address (bytes) – The IPv6 address in binary form.

Raises:

ValueError – If the address is not 16 bytes long.

Return type:

str

dns.ipv6.is_mapped(address: bytes) bool[source]

Is the specified address a mapped IPv4 address?

Parameters:

address (bytes) – An IPv6 address in binary form.

Return type:

bool

dns.ttl.from_text(text: str) int[source]

Convert the text form of a TTL to an integer.

The BIND 8 units syntax for TTLs (e.g. ‘1w6d4h3m10s’) is supported.

Parameters:

text (str) – The textual TTL.

Raises:

dns.ttl.BadTTL – If the TTL is not well-formed.

Return type:

int

class dns.set.Set(items=None)[source]

A simple set class.

This class was originally used to deal with python not having a set class, and originally the class used lists in its implementation. The ordered and indexable nature of RRsets and Rdatasets is unfortunately widely used in dnspython applications, so for backwards compatibility sets continue to be a custom class, now based on an ordered dictionary.

Initialize the set.

items, an iterable or None, the initial set of items.

add(item)[source]

Add an item to the set.

clear()[source]

Make the set empty.

copy()[source]

Make a (shallow) copy of the set.

difference(other)[source]

Return a new set which self - other, i.e. the items in self which are not also in other.

Returns the same Set type as this set.

difference_update(other)[source]

Update the set, removing any elements from other which are in the set.

discard(item)[source]

Remove an item from the set if present.

intersection(other)[source]

Return a new set which is the intersection of self and other.

Returns the same Set type as this set.

intersection_update(other)[source]

Update the set, removing any elements from other which are not in both sets.

issubset(other)[source]

Is this set a subset of other?

Return type:

bool

issuperset(other)[source]

Is this set a superset of other?

Return type:

bool

pop()[source]

Remove an arbitrary item from the set.

remove(item)[source]

Remove an item from the set.

symmetric_difference(other)[source]

Return a new set which (self - other) | (other - self), ie: the items in either ``self or other which are not contained in their intersection.

Returns the same Set type as this set.

symmetric_difference_update(other)[source]

Update the set, retaining only elements unique to both sets.

union(other)[source]

Return a new set which is the union of self and other.

Returns the same Set type as this set.

union_update(other)[source]

Update the set, adding any elements from other which are not already in the set.

update(other)[source]

Update the set, adding any elements from other which are not already in the set.

other, the collection of items with which to update the set, which may be any iterable type.

dnspython release version information.

dns.version.MAJOR = 2

MAJOR

dns.version.MICRO = 0

MICRO

dns.version.MINOR = 9

MINOR

dns.version.RELEASELEVEL = 0

RELEASELEVEL

dns.version.SERIAL = 0

SERIAL

dns.version.hexversion = 34144256

hexversion

dns.version.version = '2.9.0dev0'

version

class dns.wire.Parser(wire: bytes, current: int = 0)[source]

Initialize a Parser.

Parameters:
  • wire (bytes) – The data to be parsed (typically a whole message or a slice of it).

  • current (int) – The offset within wire where parsing should begin.