The dns.message.Message Class
This is the base class for all messages, and the class used for any DNS opcodes that do not have a more specific class.
- class dns.message.Message(id: int | None = None)[source]
A DNS message.
- id
An
int, the query id; the default is a randomly chosen id.
- flags
An
int, the DNS flags of the message.
- sections
A list of lists of
dns.rrset.RRsetobjects.
- edns
An
int, the EDNS level to use. The default is -1, no EDNS.
- ednsflags
An
int, the EDNS flags.
- payload
An
int, the EDNS payload size. The default is 0.
- options
The EDNS options, a list of
dns.edns.Optionobjects. The default is the empty list.
- request_payload
The associated request’s EDNS payload size. This field is meaningful in response messages, and if set to a non-zero value, will limit the size of the response to the specified size. The default is 0, which means “use the default limit” which is currently 65535.
- keyring
A
dns.tsig.Key, the TSIG key. The default is None.
- keyname
The TSIG keyname to use, a
dns.name.Name. The default isNone.
- keyalgorithm
A
dns.name.Name, the TSIG algorithm to use. Defaults todns.tsig.default_algorithm. Constants for TSIG algorithms are defined the indns.tsigmodule.
- request_mac
A
bytes, the TSIG MAC of the request message associated with this message; used when validating TSIG signatures.
- fudge
An
int, the TSIG time fudge. The default is 300 seconds.
- original_id
An
int, the TSIG original id; defaults to the message’s id.
- tsig_error
An
int, the TSIG error code. The default is 0.
- other_data
A
bytes, the TSIG “other data”. The default is the emptybytes.
- mac
A
bytes, the TSIG MAC for this message.
- xfr
A
bool. This attribute is true when the message being used for the results of a DNS zone transfer. The default isFalse.
- origin
A
dns.name.Name. The origin of the zone in messages which are used for zone transfers or for DNS dynamic updates. The default isNone.
- tsig_ctx
An
hmac.HMAC, the TSIG signature context associated with this message. The default isNone.
- had_tsig
A
bool, which isTrueif the message had a TSIG signature when it was decoded from wire format.
- multi
A
bool, which isTrueif this message is part of a multi-message sequence. The default isFalse. This attribute is used when validating TSIG signatures on messages which are part of a zone transfer.
- first
A
bool, which isTrueif this message is stand-alone, or the first of a multi-message sequence. The default isTrue. This variable is used when validating TSIG signatures on messages which are part of a zone transfer.
- index
A
dict, an index of RRsets in the message. The index key is(section, name, rdclass, rdtype, covers, deleting). The default is{}. Indexing improves the performance of finding RRsets. Indexing can be disabled by setting the index toNone.
- wire
A
bytesorNone, the encoded wire format data used to create this message withdns.message.from_wire()or the output most recently generated byto_wire().
- extended_errors() list[EDEOption][source]
Return the list of Extended DNS Error (EDE) options in the message
- find_rrset(section: int | str | list[RRset], name: Name, rdclass: RdataClass, rdtype: RdataType, covers: RdataType = RdataType.TYPE0, deleting: RdataClass | None = None, create: bool = False, force_unique: bool = False, idna_codec: IDNACodec | None = None) RRset[source]
Find the RRset with the given attributes in the specified section.
section may be an
intsection number, astrsection name, or one of the section list attributes of this message. For example:my_message.find_rrset(my_message.answer, name, rdclass, rdtype) my_message.find_rrset(dns.message.ANSWER, name, rdclass, rdtype) my_message.find_rrset("ANSWER", name, rdclass, rdtype)
- Parameters:
name (
dns.name.Nameorstr) – The owner name.rdclass (
dns.rdataclass.RdataClassorstr) – The rdata class.rdtype (
dns.rdatatype.RdataTypeorstr) – The rdata type.covers (
dns.rdatatype.RdataTypeorstr) – The covered type; default isdns.rdatatype.NONE.deleting (
dns.rdataclass.RdataClass,str, orNone) – The deleting value; default isNone.create (bool) – If
True, create and append the RRset if not found.force_unique (bool) – If
Trueand create isTrue, always create a new RRset even if a matching one exists. Useful for DDNS updates.idna_codec (
dns.name.IDNACodecorNone) – The IDNA encoder/decoder. Defaults to IDNA 2003.
- Raises:
KeyError – If the RRset was not found and create is
False.- Return type:
- get_options(otype: OptionType) list[Option][source]
Return the list of options of the specified type.
- get_rrset(section: int | str | list[RRset], name: Name, rdclass: RdataClass, rdtype: RdataType, covers: RdataType = RdataType.TYPE0, deleting: RdataClass | None = None, create: bool = False, force_unique: bool = False, idna_codec: IDNACodec | None = None) RRset | None[source]
Get the RRset with the given attributes in the specified section.
Like
find_rrset()but returnsNoneinstead of raisingKeyErrorwhen the RRset is not found.section may be an
intsection number, astrsection name, or one of the section list attributes of this message.- Parameters:
name (
dns.name.Nameorstr) – The owner name.rdclass (
dns.rdataclass.RdataClassorstr) – The rdata class.rdtype (
dns.rdatatype.RdataTypeorstr) – The rdata type.covers (
dns.rdatatype.RdataTypeorstr) – The covered type; default isdns.rdatatype.NONE.deleting (
dns.rdataclass.RdataClass,str, orNone) – The deleting value; default isNone.create (bool) – If
True, create and append the RRset if not found.force_unique (bool) – If
Trueand create isTrue, always create a new RRset even if a matching one exists.idna_codec (
dns.name.IDNACodecorNone) – The IDNA encoder/decoder. Defaults to IDNA 2003.
- Returns:
The matching RRset, or
Noneif not found.- Return type:
dns.rrset.RRsetorNone
- is_response(other: Message) bool[source]
Is other a response to this message?
- Parameters:
other (
dns.message.Message) – The message to check.- Return type:
- section_count(section: int | str | list[RRset]) int[source]
Returns the number of records in the specified section.
- Parameters:
section –
An
intsection number, astrsection name, or one of the section attributes of this message. For example:my_message.section_count(my_message.answer) my_message.section_count(dns.message.ANSWER) my_message.section_count("ANSWER")
- section_from_number(number: int) list[RRset][source]
Return the section list associated with the specified section number.
- Parameters:
number – A section number (
int) or text name of a section.- Raises:
ValueError – If the section is not known.
- Return type:
- section_number(section: list[RRset]) int[source]
Return the “section number” of the specified section for use in indexing.
- Parameters:
section – One of the section attributes of this message.
- Raises:
ValueError – If the section is not known.
- Return type:
- set_opcode(opcode: Opcode) None[source]
Set the opcode.
- Parameters:
opcode (
dns.opcode.Opcode) – The opcode to set.
- to_styled_text(style: MessageStyle) str[source]
Convert the message to styled text.
- Parameters:
style (
dns.message.MessageStyle) – The style to use for formatting.- Return type:
- to_text(origin: Name | None = None, relativize: bool = True, style: MessageStyle | None = None, **kw: Any) str[source]
Convert the message to text.
The origin, relativize, and any other keyword arguments are passed to the RRset
to_text()method.- Parameters:
style (
dns.rdataset.RdatasetStyleorNone) – If specified, overrides origin and relativize.- Return type:
- to_wire(origin: Name | None = None, max_size: int = 0, multi: bool = False, tsig_ctx: Any | None = None, prepend_length: bool = False, prefer_truncation: bool = False, **kw: Any) bytes[source]
Return the message in DNS compressed wire format.
Additional keyword arguments are passed to the RRset
to_wire()method.- Parameters:
origin (
dns.name.NameorNone) – Origin to append to relative names. IfNone, the message’s own origin (if any) is used.max_size (int) – Maximum wire format size; 0 means use the request payload or 65535.
multi (bool) –
Trueif this message is part of a multi-message sequence.tsig_ctx – Ongoing TSIG context for zone transfer signing.
prepend_length (bool) – If
True, prepend the 2-byte message length (useful for TCP/TLS/QUIC).prefer_truncation (bool) – If
True, truncate instead of raising when the message exceeds max_size. Sets TC if truncation is before the additional section.
- Raises:
dns.exception.TooBig – If max_size is exceeded and prefer_truncation is
False.- Return type:
- use_edns(edns: int | bool | None = 0, ednsflags: int = 0, payload: int = 1232, request_payload: int | None = None, options: list[Option] | None = None, pad: int = 0) None[source]
Configure EDNS behavior.
- Parameters:
edns (int or
None) – The EDNS level to use. SpecifyingNone,False, or-1means “do not use EDNS” (other parameters are ignored). SpecifyingTrueis equivalent to specifying 0 (use EDNS0).ednsflags (int) – The EDNS flag values.
payload (int) – The EDNS sender’s payload field — the maximum UDP datagram size the sender can handle (i.e. how big a response can be).
request_payload (int or
None) – The EDNS payload size to use when sending. Defaults to the value of payload.options (list of
dns.edns.OptionorNone) – The EDNS options.pad (int) – If 0 (the default), do not pad; otherwise add padding bytes to make the message size a multiple of pad. When nonzero, an EDNS PADDING option is always added.
- use_tsig(keyring: Any, keyname: Name | str | None = None, fudge: int = 300, original_id: int | None = None, tsig_error: int = 0, other_data: bytes = b'', algorithm: Name | str = <DNS name hmac-sha256.>) None[source]
Arrange for a TSIG signature to be added when sending.
- Parameters:
keyring (dict, callable, or
dns.tsig.Key) – The TSIG keyring or key. Adictmapsdns.name.Namekeys todns.tsig.Keyobjects orbytessecrets. If adictis given without keyname, the first key in the dict is used. A callable is invoked with the message and keyname and must return a key.keyname (
dns.name.Name,str, orNone) – The TSIG key name. Ignored if keyring is adns.tsig.Key. Defaults toNone.fudge (int) – The TSIG time fudge.
original_id (int or
None) – The TSIG original id. Defaults to the message id.tsig_error (int) – The TSIG error code.
other_data (bytes) – The TSIG other data.
algorithm (
dns.name.Nameorstr) – The TSIG algorithm. Only used when keyring is adictand the key entry isbytes.
- class dns.message.MessageStyle(omit_final_dot: bool = False, idna_codec: IDNACodec | None = None, origin: Name | None = None, relativize: bool = False, txt_is_utf8: bool = False, base64_chunk_size: int = 32, base64_chunk_separator: str = ' ', hex_chunk_size: int = 128, hex_chunk_separator: str = ' ', truncate_crypto: bool = False, override_rdclass: RdataClass | None = None, want_comments: bool = False, omit_rdclass: bool = False, omit_ttl: bool = False, want_generic: bool = False, deduplicate_names: bool = False, first_name_is_duplicate: bool = False, default_ttl: int | None = None, name_just: int = 0, ttl_just: int = 0, rdclass_just: int = 0, rdtype_just: int = 0)[source]
Message text styles.
A
MessageStyleis also adns.name.NameStyleand adns.rdata.RdataStyle, and adns.rdataset.RdatasetStyle. See those classes for a description of their options.There are currently no message-specific style options, but if that changes they will be documented here.
The following constants may be used to specify sections in the
find_rrset() and get_rrset() methods:
- dns.message.QUESTION = MessageSection.QUESTION
Message sections
- dns.message.ANSWER = MessageSection.ANSWER
Message sections
- dns.message.AUTHORITY = MessageSection.AUTHORITY
Message sections
- dns.message.ADDITIONAL = MessageSection.ADDITIONAL
Message sections