The dns.resolver.Resolver and dns.resolver.Answer Classes

classdns.resolver.Resolver(filename:str='/etc/resolv.conf',configure:bool=True)[source]

DNS stub resolver.

filename, astr or file object, specifying a filein standard /etc/resolv.conf format. This parameter is meaningfulonly whenconfigure is true and the platform is POSIX.

configure, abool. If True (the default), the resolverinstance is configured in the normal fashion for the operatingsystem the resolver is running on. (I.e. by reading a/etc/resolv.conf file on POSIX systems and from the registryon Windows systems.)

domain

Adns.name.Name, the domain of this host.

nameservers

Alist ofstr ordns.nameserver.Nameserver. A string may bean IPv4 or IPv6 address, or an https URL.

This field is actually a property, and returns a tuple as of dnspython 2.4.Assigning this field converts any strings intodns.nameserver.Nameserver instances.

search

Alist of dns.name.Name objects. If the query name is arelative name, the resolver will construct absolute query namesto try by appending values from the search list.

use_search_by_default

Abool, specifes whether or notresolve() uses thesearch list configured in the system’s resolver configurationwhen thesearch parameter toresolve() isNone. Thedefault isFalse.

port

Anint, the default DNS port to send to if not overridden bynameserver_ports. The default value is 53.

nameserver_ports

Adict mapping an IPv4 or IPv6 addressstr to anint.This specifies the port to use when sending to a nameserver. Ifa port is not defined for an address, the value of theportattribute will be used.

timeout

Afloat, the number of seconds to wait for a response froma server.

lifetime

Afloat, the number of seconds to spend trying to get ananswer to the question. If the lifetime expires adns.exception.Timeout exception will be raised.

cache

An object implementing the caching protocol, e.g. adns.resolver.Cache or adns.resolver.LRUCache. The defaultisNone, in which case there is no local caching.

retry_servfail

Abool. Should we retry a nameserver if it saysSERVFAIL?The default isFalse.

keyring

Adict, the TSIG keyring to use. If akeyring isspecified but akeyname is not, then the key used will bethe first key in thekeyring. Note that the order of keysin a dictionary is not defined, so applications should supplya keyname when a keyring is used, unless they know the keyringcontains only one key.

keyname

Adns.name.Name orNone, the name of the TSIG key touse; defaults toNone. The key must be defined in thekeyring.

keyalgorithm

Adns.name.Name orstr, the TSIG algorithm to use.

edns

Anint, the EDNS level to use. SpecifyingNone,False, or-1 means “do not use EDNS”, and inthis case the other parameters are ignored. SpecifyingTrue is equivalent to specifying 0, i.e. “use EDNS0”.

ednsflags

Anint, the EDNS flag values.

payload

Anint, is the EDNS sender’s payload field, which is themaximum size of UDP datagram the sender can handle. I.e. how biga response to this message can be.

flags

Anint orNone, the message flags to use. IfNone,then the default flags as set by thedns.message.Messageconstructor will be used.

canonical_name(name:Name|str)Name[source]

Determine the canonical name ofname.

The canonical name is the name the resolver uses for queriesafter all CNAME and DNAME renamings have been applied.

name, adns.name.Name orstr, the query name.

This method can raise any exception thatresolve() canraise, other thandns.resolver.NoAnswer anddns.resolver.NXDOMAIN.

Returns adns.name.Name.

query(qname:Name|str,rdtype:RdataType|str=RdataType.A,rdclass:RdataClass|str=RdataClass.IN,tcp:bool=False,source:str|None=None,raise_on_no_answer:bool=True,source_port:int=0,lifetime:float|None=None)Answer[source]

Query nameservers to find the answer to the question.

This method calls resolve() withsearch=True, and isprovided for backwards compatibility with prior versions ofdnspython. See the documentation for thedns.resolver.Resolver.resolve() method for further details.

resolve(qname:Name|str,rdtype:RdataType|str=RdataType.A,rdclass:RdataClass|str=RdataClass.IN,tcp:bool=False,source:str|None=None,raise_on_no_answer:bool=True,source_port:int=0,lifetime:float|None=None,search:bool|None=None)Answer[source]

Query nameservers to find the answer to the question.

Theqname,rdtype, andrdclass parameters may be objectsof the appropriate type, or strings that can be converted into objectsof the appropriate type.

qname, adns.name.Name orstr, the query name.

rdtype, anint orstr, the query type.

rdclass, anint orstr, the query class.

tcp, abool. IfTrue, use TCP to make the query.

source, astr orNone. If notNone, bind to this IPaddress when making queries.

raise_on_no_answer, abool. IfTrue, raisedns.resolver.NoAnswer if there’s no answer to the question.

source_port, anint, the port from which to send the message.

lifetime, afloat, how many seconds a query should runbefore timing out.

search, abool orNone, determines whether thesearch list configured in the system’s resolver configurationare used for relative names, and whether the resolver’s domainmay be added to relative names. The default isNone,which causes the value of the resolver’suse_search_by_default attribute to be used.

Raisesdns.resolver.LifetimeTimeout if no answers could be foundin the specified lifetime.

Raisesdns.resolver.NXDOMAIN if the query name does not exist.

Raisesdns.resolver.YXDOMAIN if the query name is too long afterDNAME substitution.

Raisesdns.resolver.NoAnswer ifraise_on_no_answer isTrue and the query name exists but has no RRset of thedesired type and class.

Raisesdns.resolver.NoNameservers if no non-brokennameservers are available to answer the question.

Returns adns.resolver.Answer instance.

resolve_address(ipaddr:str,*args:Any,**kwargs:Any)Answer[source]

Use a resolver to run a reverse query for PTR records.

This utilizes the resolve() method to perform a PTR lookup on thespecified IP address.

ipaddr, astr, the IPv4 or IPv6 address you want to getthe PTR record for.

All other arguments that can be passed to the resolve() functionexcept for rdtype and rdclass are also supported by thisfunction.

resolve_name(name:Name|str,family:int=AddressFamily.AF_UNSPEC,**kwargs:Any)HostAnswers[source]

Use a resolver to query for address records.

This utilizes the resolve() method to perform A and/or AAAA lookups onthe specified name.

qname, adns.name.Name orstr, the name to resolve.

family, anint, the address family. If socket.AF_UNSPEC(the default), both A and AAAA records will be retrieved.

All other arguments that can be passed to the resolve() functionexcept for rdtype and rdclass are also supported by thisfunction.

try_ddr(lifetime:float=5.0)None[source]

Try to update the resolver’s nameservers using Discovery of DesignatedResolvers (DDR). If successful, the resolver will subsequently useDNS-over-HTTPS or DNS-over-TLS for future queries.

lifetime, a float, is the maximum time to spend attempting DDR. The defaultis 5 seconds.

If the SVCB query is successful and results in a non-empty list of nameservers,then the resolver’s nameservers are set to the returned servers in priorityorder.

The current implementation does not use any address hints from the SVCB record,nor does it resolve addresses for the SCVB target name, rather it assumes thatthe bootstrap nameserver will always be one of the addresses and uses it.A future revision to the code may offer fuller support. The code verifies thatthe bootstrap nameserver is in the Subject Alternative Name field of theTLS certficate.

classdns.resolver.Answer(qname:Name,rdtype:RdataType,rdclass:RdataClass,response:QueryMessage,nameserver:str|None=None,port:int|None=None)[source]

DNS stub resolver answer.

Instances of this class bundle up the result of a successful DNSresolution.

For convenience, the answer object implements much of the sequenceprotocol, forwarding to itsrrset attribute. E.g.forainanswer is equivalent toforainanswer.rrset.answer[i] is equivalent toanswer.rrset[i], andanswer[i:j] is equivalent toanswer.rrset[i:j].

Note that CNAMEs or DNAMEs in the response may mean that answerRRset’s name might not be the query name.

qname

Adns.name.Name, the query name.

rdclass

Anint, the query class.

rdtype

Anint, the query type.

response

Adns.message.QueryMessage, the response message.

rrset

Adns.rrset.RRset orNone, the answer RRset.

expiration

Afloat, the time when the answer expires.

canonical_name

Adns.name.Name, the canonical name of the query name,i.e. the owner name of the answer RRset after any CNAME and DNAMEchaining.