can i write a dns zone file with ttl using dnspython?

dnspython library, as of v2.1.0rc1 (the latest version as of November, 9, 2020), doesn’t support this type of a zone style per se. Going down your stack trace for the to_text() call:

  1. dns/ to_text()
  2. dns/ to_file()
  3. dns/ to_text()
  4. dns/ to_text()

— this is where the actual formatting is being done:

for rd in self:
    extra = ''
    if want_comments:
        if rd.rdcomment:
            extra = f' ;{rd.rdcomment}'
        s.write('%s%s%d %s %s %s%s\n' %
                (ntext, pad, self.ttl, dns.rdataclass.to_text(rdclass),
                 rd.to_text(origin=origin, relativize=relativize,

So, as you can see, self.ttl is included into the rdataset text output invariably.

What you can do though is iterate over the zone, creating a zone file of your preferred style yourself — or make changes directly to the to_text() output which is a standardized zone description and thus is stable enough to be processed automatically. Quick example of how that could look:

zone_lines = ['$TTL %s' % zone['@'].rdatasets[0].ttl]
for rdat in zone.to_text().decode('ascii').strip().split('\n'):
    rdat_params = rdat.split(' ')
    zone_lines.append(' '.join(rdat_params))

zone_text = '\n'.join(zone_lines)

You can also create a feature request for the dnspython project on Github. The author would then consider your feature request, they have been quite responsive recently.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top