Making DNS Messages

exception dns.message.Truncated(*args, **kwargs)[source]

The truncated flag is set.

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.IDNACodec or None) – The IDNA encoder/decoder. Defaults to IDNA 2003.

  • one_rr_per_rrset (bool) – If True, put each RR into its own RRset.

Raises:
Return type:

dns.message.Message

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.IDNACodec or None) – 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.Name or None) – The origin to use for relative names.

  • relativize (bool) – If True, names will be relativized.

  • relativize_to (dns.name.Name or None) – The origin to relativize to. Defaults to origin.

Raises:
Return type:

dns.message.Message

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, or None) – The key or keyring for TSIG validation. None or True causes TSIG-signed messages to fail; False disables validation.

  • request_mac (bytes or None) – MAC of the TSIG-signed request this message responds to, if any.

  • xfr (bool) – True if this message is part of a zone transfer.

  • origin (dns.name.Name or None) – Zone origin for zone transfers; names are relativized to this if not None.

  • tsig_ctx – Ongoing TSIG context for zone transfer validation.

  • multi (bool) – True if 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, raise dns.message.Truncated when the TC bit is set.

  • continue_on_error (bool) – If True, try to continue parsing on errors and accumulate them in the message’s errors attribute.

Raises:
Return type:

dns.message.Message

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.Name or str) – The query name.

  • rdtype (dns.rdatatype.RdataType or str) – The desired rdata type.

  • rdclass (dns.rdataclass.RdataClass or str) – The desired rdata class; default is IN.

  • use_edns (int, bool, or None) – The EDNS level; None enables EDNS only if other EDNS parameters are set. See dns.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.Option or None) – The EDNS options.

  • idna_codec (dns.name.IDNACodec or None) – 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.QueryMessage

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 not None pad to a multiple of this value; if None, follow RFC 8467 (pad to 468 if request was padded).

  • copy_mode (dns.message.CopyMode or None) – Controls which sections are copied. None uses the default for the opcode (currently dns.message.CopyMode.QUESTION).

Returns:

A response message of the same class as query.

Return type:

dns.message.Message