The RRSet Reader
dns.zonefile.read_rrsets() reads one or more RRsets from text format. It
is designed to be used in situations where you are processing DNS data in
text format, but do not want or need a valid zone. For example, a DNS registry
web application might want to allow the user to input RRs.
- dns.zonefile.read_rrsets(text: Any, name: Name | str | None = None, ttl: int | None = None, rdclass: RdataClass | str | None = RdataClass.IN, default_rdclass: RdataClass | str = RdataClass.IN, rdtype: RdataType | str | None = None, default_ttl: int | str | None = None, idna_codec: IDNACodec | None = None, origin: Name | str | None = <DNS name .>, relativize: bool = False) list[RRset][source]
Read one or more rrsets from the specified text, possibly subject to restrictions.
- Parameters:
text – The input to process, either a file object or a string.
name (str,
dns.name.Name, orNone) – The owner name of the rrset. If notNone, the owner name is forced and the input must not specify one. IfNone, any owner names are allowed and must be present in the input.ttl (int, str, or
None) – If notNone, the TTL is forced to the specified value and the input must not specify a TTL. IfNone, a TTL may be specified in the input; if not specified, default_ttl will be used.rdclass (
dns.rdataclass.RdataClass, str, orNone) – If notNone, the class is forced to the specified value and the input must not specify a class. IfNone, the input may optionally specify a class matching default_rdclass. Note that rrsets with differing classes cannot be returned;Nonehere simply allows the user to optionally write a class in the input.default_rdclass (
dns.rdataclass.RdataClassor str) – The class of the returned rrsets.rdtype (
dns.rdatatype.RdataType, str, orNone) – If notNone, the type is forced to the specified value and the input must not specify a type. IfNone, a type must be present for each RR.default_ttl (int, str, or
None) – If notNone, and the TTL is not forced and not specified in the input, this value will be used. IfNone, an error will occur if the TTL is not forced and not specified.idna_codec (
dns.name.IDNACodecorNone) – The IDNA encoder/decoder. IfNone, the default IDNA encoder/decoder is used. Note that codecs only apply to the owner name; dnspython does not do IDNA for names in rdata.origin (str,
dns.name.Name, orNone) – The origin for any relative names in the input, and also the origin to relativize to if relativize isTrue.relativize (bool) – If
True, names are relativized to origin; ifFalse, relative names in the input are made absolute by appending origin.
- Return type:
list[
dns.rrset.RRset]
Examples
Read RRSets with name, TTL, and rdclass forced:
input = '''
mx 10 a
mx 20 b
ns ns1
'''
rrsets = dns.zonefile.read_rrsets(input, name='name', ttl=300)
Read RRSets with name, TTL, rdclass, and rdtype forced:
input = '''
10 a
20 b
'''
rrsets = dns.zonefile.read_rrsets(input, name='name', ttl=300, rdtype='mx')
Note that in this case the length of rrsets will always be one.
Read relativized RRsets with unforced rdclass (but which must match default_rdclass):
input = '''
name1 20 MX 10 a.example.
name2 30 IN MX 20 b
'''
rrsets = dns.zonefile.read_rrsets(input, origin='example', relativize=True,
rdclass=None)
The dns.zonefile.Reader Class
The Reader class reads data in DNS zonefile format, or various
restrictions of that format, and converts it to a sequence of operations
in a transaction.
This class is primarily used by dns.zone.from_text() and
dns.zonefile.read_rrsets(), but may be useful for other software which needs
to process the zonefile format.
- class dns.zonefile.Reader(tok: Tokenizer, rdclass: RdataClass, txn: Transaction, allow_include: bool = False, allow_directives: bool | Iterable[str] = True, force_name: Name | None = None, force_ttl: int | None = None, force_rdclass: RdataClass | None = None, force_rdtype: RdataType | None = None, default_ttl: int | None = None)[source]
Read a DNS zone file into a transaction.
- read() None[source]
Read a DNS zone file and build a zone object.
- Raises:
dns.zone.NoSOA – if there is no SOA RR at the zone origin.
dns.zone.NoNS – if there is no NS RRset at the zone origin.