Validate DNS txt record entry for a domain
Sun, 02 April 2017
requirements.txt
dnspython
dns_lookup_service.py
import dns.resolver from dns.rdtypes.ANY import TXT import logging class DnsLookupService(object): @staticmethod def has_txt_record(domain, record_to_check): """ Check a domains TXT record for a given record. :param str domain: :param str record_to_check: :rtype: bool """ resolve = dns.resolver.Resolver() resolve.nameservers = ['8.8.8.8', '8.8.4.4'] answers = resolve.query(domain, "TXT") for piece in answers: # type: TXT try: single = unicode(piece.to_text()) if single[:1] == "\\\\"" and single[(len(single)-1):] == "\\\\"": single = single[1:(len(single)-1)] if single == unicode(record_to_check): return True except Exception, e: # More than likely a unicode decode error. logging.exception(e) pass return False
usage.py
from dns_lookup_service import DnsLookupService DnsLookupService.has_txt_record('example.com', 'foo=bar') # bool