dns

DNS module.

exception wmflib.dns.DnsError[source]

Bases: WmflibError

Custom exception class for errors of the Dns class.

__doc__ = 'Custom exception class for errors of the Dns class.'
__module__ = 'wmflib.dns'
exception wmflib.dns.DnsNotFoundError[source]

Bases: DnsError

Custom exception class to indicate the record was not found.

One or more resource records might exist for this domain but no record matches the resource record type requested.

__doc__ = 'Custom exception class to indicate the record was not found.\n\n    One or more resource records might exist for this domain but no record matches the resource record type requested.\n    '
__module__ = 'wmflib.dns'
wmflib.dns.DnsNotFound

alias of DnsNotFoundError

class wmflib.dns.Dns(*, nameserver_addresses: Sequence[str] | None = None, port: int | None = None)[source]

Bases: object

Class to interact with the DNS.

Initialize the instance optionally specifying the nameservers to use.

Examples

Using the host’s default DNS resolvers:

>>> from wmflib.dns import Dns
>>> dns = Dns()

Using a specific set of resolvers and port:

>>> from wmflib.dns import Dns
>>> dns = Dns(nameserver_addresses=["10.0.0.1", "10.0.0.2"], port=5353)
Parameters:
  • nameserver_addresses (Sequence, optional) – the nameserveres address to use, if not set uses the OS configuration.

  • port (int, optional) – the port the nameserver_addresses nameserveres is listening to, if different from the default 53. This applies only if a nameserveres is explicitelyes specified.

__init__(*, nameserver_addresses: Sequence[str] | None = None, port: int | None = None) None[source]

Initialize the instance optionally specifying the nameservers to use.

Examples

Using the host’s default DNS resolvers:

>>> from wmflib.dns import Dns
>>> dns = Dns()

Using a specific set of resolvers and port:

>>> from wmflib.dns import Dns
>>> dns = Dns(nameserver_addresses=["10.0.0.1", "10.0.0.2"], port=5353)
Parameters:
  • nameserver_addresses (Sequence, optional) – the nameserveres address to use, if not set uses the OS configuration.

  • port (int, optional) – the port the nameserver_addresses nameserveres is listening to, if different from the default 53. This applies only if a nameserveres is explicitelyes specified.

resolve_ipv4(name: str) List[str][source]

Perform a DNS lookup for an A record for the given name.

Examples

>>> dns.resolve_ipv4("api.svc.eqiad.wmnet")
['10.2.2.22']
Parameters:

name (str) – the name to resolve.

Returns:

the list of IPv4 addresses as strings returned by the DNS response.

Return type:

list

resolve_ipv6(name: str) List[str][source]

Perform a DNS lookup for an AAAA record for the given name.

Examples

>>> dns.resolve_ipv6("wikimedia.org")
['2620:0:861:ed1a::1']
Parameters:

name (str) – the name to resolve.

Returns:

the list of IPv6 addresses as strings returned by the DNS response.

Return type:

list

resolve_ips(name: str) List[str][source]

Perform a DNS lookup for A and AAAA records for the given name.

Examples

>>> dns.resolve_ips("wikimedia.org")
['208.80.154.224', '2620:0:861:ed1a::1']
Parameters:

name (str) – the name to resolve.

Returns:

the list of IPv4 and IPv6 addresses as strings returned by the DNS response.

Return type:

list

Raises:

wmflib.dns.DnsNotFoundError – when no address is found.

resolve_ptr(address: str) List[str][source]

Perform a DNS lookup for PTR record for the given address.

Examples

>>> dns.resolve_ptr("208.80.154.224")
['text-lb.eqiad.wikimedia.org']
Parameters:

address (str) – the IPv4 or IPv6 address to resolve.

Returns:

the list of absolute target PTR records as strings, without the trailing dot.

Return type:

list

resolve_cname(name: str) str[source]

Perform a DNS lookup for CNAME record for the given name.

Examples

>>> dns.resolve_cname("puppet.codfw.wmnet")
'puppetmaster2001.codfw.wmnet'
Parameters:

name (str) – the name to resolve.

Returns:

the absolute target name for this CNAME, without the trailing dot.

Return type:

str

resolve(qname: str | Name, record_type: str) Answer[source]

Perform a DNS lookup for the given qname and record type.

Examples

>>> response = dns.resolve("wikimedia.org", "MX")
>>> [rdata.to_text() for rdata in response.rrset]
['10 mx1001.wikimedia.org.', '50 mx2001.wikimedia.org.']
Parameters:
  • qname (str) – the name or address to resolve.

  • record_type (str) – the DNS record type to lookup for, like ‘A’, ‘AAAA’, ‘PTR’, etc.

Returns:

the DNS response.

Return type:

dns.resolver.Answer

Raises:
_resolve_addresses(name: str, record_type: str) List[str][source]

Extract and return all the matching addresses for the given name and record type.

Parameters:
  • name (str) – the name to resolve.

  • record_type (str) – the DNS record type to lookup for, like ‘A’ and ‘AAAA’.

Returns:

the list of IPv4 or IPv6 addresses as strings returned by the DNS response.

Return type:

list

static _parse_targets(response_set: RRset) List[str][source]

Extract and return all the matching names from the given rrset without the trailing dot.

Parameters:

response_set (dns.rrset.RRset) – the RRset to parse.

Returns:

the list of absolute target record names as strings without the trailing dot.

Return type:

list

Raises:

wmflib.dns.DnsError – if a relative record is found.

__dict__ = mappingproxy({'__module__': 'wmflib.dns', '__doc__': 'Class to interact with the DNS.', '__init__': <function Dns.__init__>, 'resolve_ipv4': <function Dns.resolve_ipv4>, 'resolve_ipv6': <function Dns.resolve_ipv6>, 'resolve_ips': <function Dns.resolve_ips>, 'resolve_ptr': <function Dns.resolve_ptr>, 'resolve_cname': <function Dns.resolve_cname>, 'resolve': <function Dns.resolve>, '_resolve_addresses': <function Dns._resolve_addresses>, '_parse_targets': <staticmethod object>, '__dict__': <attribute '__dict__' of 'Dns' objects>, '__weakref__': <attribute '__weakref__' of 'Dns' objects>, '__annotations__': {}})
__doc__ = 'Class to interact with the DNS.'
__module__ = 'wmflib.dns'
__weakref__

list of weak references to the object (if defined)

class wmflib.dns.PublicAuthDns[source]

Bases: Dns

Class to interact with the DNS using the wikimedia foundation authoritative servers.

Initialize the instance with the WMF public authoritative namerservers.

It uses the nameservers defined in wmflib.constants.PUBLIC_AUTHDNS.

Examples

>>> from wmflib.dns import PublicAuthDns
>>> dns = PublicAuthDns()
__doc__ = 'Class to interact with the DNS using the wikimedia foundation authoritative servers.'
__init__() None[source]

Initialize the instance with the WMF public authoritative namerservers.

It uses the nameservers defined in wmflib.constants.PUBLIC_AUTHDNS.

Examples

>>> from wmflib.dns import PublicAuthDns
>>> dns = PublicAuthDns()
__module__ = 'wmflib.dns'