Making DNS Messages
- class dns.message.CopyMode(value)[source]
How should sections be copied when making an update response?
- EVERYTHING = 2
Copy all sections, other than OPT and TSIG RRs.
- NOTHING = 0
Copy no sections; only suitable for testing
- QUESTION = 1
Copy only the question section, if present.
- dns.message.from_file(f: Any, idna_codec: IDNACodec | None = None, one_rr_per_rrset: bool = False) Message[source]
Read the next text format message from the specified file.
Message blocks are separated by a single blank line.
- Parameters:
f – A file object or a pathname string.
idna_codec (
dns.name.IDNACodecorNone) – The IDNA encoder/decoder. Defaults to IDNA 2003.one_rr_per_rrset (bool) – If
True, put each RR into its own RRset.
- Raises:
dns.message.UnknownHeaderField – If a header field is unknown.
dns.exception.SyntaxError – If the text is badly formed.
- Return type:
- dns.message.from_text(text: str, idna_codec: IDNACodec | None = None, one_rr_per_rrset: bool = False, origin: Name | None = None, relativize: bool = True, relativize_to: Name | None = None) Message[source]
Convert the text format message into a message object.
The reader stops after reading the first blank line in the input to facilitate reading multiple messages from a single file with
dns.message.from_file().- Parameters:
text (str) – The text format message.
idna_codec (
dns.name.IDNACodecorNone) – The IDNA encoder/decoder. Defaults to IDNA 2003.one_rr_per_rrset (bool) – If
True, put each RR into its own RRset.origin (
dns.name.NameorNone) – The origin to use for relative names.relativize (bool) – If
True, names will be relativized.relativize_to (
dns.name.NameorNone) – The origin to relativize to. Defaults to origin.
- Raises:
dns.message.UnknownHeaderField – If a header field is unknown.
dns.exception.SyntaxError – If the text is badly formed.
- Return type:
- dns.message.from_wire(wire: bytes, keyring: Any | None = None, request_mac: bytes | None = b'', xfr: bool = False, origin: Name | None = None, tsig_ctx: HMACTSig | GSSTSig | None = None, multi: bool = False, question_only: bool = False, one_rr_per_rrset: bool = False, ignore_trailing: bool = False, raise_on_truncation: bool = False, continue_on_error: bool = False) Message[source]
Convert a DNS wire format message into a message object.
- Parameters:
keyring (
dns.tsig.Key, dict, bool, orNone) – The key or keyring for TSIG validation.NoneorTruecauses TSIG-signed messages to fail;Falsedisables validation.request_mac (bytes or
None) – MAC of the TSIG-signed request this message responds to, if any.xfr (bool) –
Trueif this message is part of a zone transfer.origin (
dns.name.NameorNone) – Zone origin for zone transfers; names are relativized to this if notNone.tsig_ctx – Ongoing TSIG context for zone transfer validation.
multi (bool) –
Trueif this message is part of a multi-message sequence.question_only (bool) – If
True, read only the question section.one_rr_per_rrset (bool) – If
True, put each RR into its own RRset.ignore_trailing (bool) – If
True, ignore trailing octets after the message.raise_on_truncation (bool) – If
True, raisedns.message.Truncatedwhen the TC bit is set.continue_on_error (bool) – If
True, try to continue parsing on errors and accumulate them in the message’serrorsattribute.
- Raises:
dns.message.ShortHeader – If the message is less than 12 octets.
dns.message.TrailingJunk – If trailing octets are present and ignore_trailing is
False.dns.message.BadEDNS – If an OPT record is in the wrong section.
dns.message.BadTSIG – If a TSIG record is not the last additional record.
dns.message.Truncated – If the TC flag is set and raise_on_truncation is
True.
- Return type:
- dns.message.make_query(qname: Name | str, rdtype: RdataType | str, rdclass: RdataClass | str = RdataClass.IN, use_edns: int | bool | None = None, want_dnssec: bool = False, ednsflags: int | None = None, payload: int | None = None, request_payload: int | None = None, options: list[Option] | None = None, idna_codec: IDNACodec | None = None, id: int | None = None, flags: int = <Flag.RD: 256>, pad: int = 0) QueryMessage[source]
Make a query message.
The query name, type, and class may be specified as objects of the appropriate type or as strings. The query id is chosen at random, and the DNS flags are set to
dns.flags.RD.- Parameters:
qname (
dns.name.Nameorstr) – The query name.rdtype (
dns.rdatatype.RdataTypeorstr) – The desired rdata type.rdclass (
dns.rdataclass.RdataClassorstr) – The desired rdata class; default is IN.use_edns (int, bool, or
None) – The EDNS level;Noneenables EDNS only if other EDNS parameters are set. Seedns.message.Message.use_edns().want_dnssec (bool) – If
True, DNSSEC data is desired.ednsflags (int) – The EDNS flag values.
payload (int) – The EDNS sender payload field (max UDP response size).
request_payload (int) – The EDNS payload size to advertise when sending. Defaults to payload.
options (list of
dns.edns.OptionorNone) – The EDNS options.idna_codec (
dns.name.IDNACodecorNone) – The IDNA encoder/decoder. Defaults to IDNA 2003.id (int or
None) – The query id. Defaults to a random id.flags (int) – The query flags. Default is
dns.flags.RD.pad (int) – If non-zero, add EDNS PADDING to make size a multiple of this.
- Return type:
- dns.message.make_response(query: Message, recursion_available: bool = False, our_payload: int = 8192, fudge: int = 300, tsig_error: int = 0, pad: int | None = None, copy_mode: CopyMode | None = None) Message[source]
Make a response skeleton for the specified query.
The returned message has all required response infrastructure but no content. Copied sections are shallow copies, so the query’s RRsets should not be changed.
- Parameters:
query (
dns.message.Message) – The query to respond to.recursion_available (bool) – If
True, set the RA bit.our_payload (int) – The EDNS payload size to advertise.
fudge (int) – The TSIG time fudge.
tsig_error (int) – The TSIG error code.
pad (int or
None) – If 0, no padding; if notNonepad to a multiple of this value; ifNone, follow RFC 8467 (pad to 468 if request was padded).copy_mode (
dns.message.CopyModeorNone) – Controls which sections are copied.Noneuses the default for the opcode (currentlydns.message.CopyMode.QUESTION).
- Returns:
A response message of the same class as query.
- Return type: