1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 __all__ = [
24 'get_txt'
25 ]
26
27
29 """Return a TXT record associated with a DNS name."""
30 try:
31 a = dns.resolver.query(name, dns.rdatatype.TXT,raise_on_no_answer=False, lifetime=timeout)
32 for r in a.response.answer:
33 if r.rdtype == dns.rdatatype.TXT:
34 return b"".join(r.items[0].strings)
35 except dns.resolver.NXDOMAIN: pass
36 return None
37
38
40 """Return a TXT record associated with a DNS name."""
41
42 if name.endswith('.'):
43 name = name[:-1]
44 response = DNS.DnsRequest(name, qtype='txt', timeout=timeout).req()
45 if not response.answers:
46 return None
47 for answer in response.answers:
48 if answer['typename'].lower() == 'txt':
49 return b''.join(answer['data'])
50 return None
51
52
53
55 """Return a TXT record associated with a DNS name."""
56
57 if name.endswith('.'):
58 name = name[:-1]
59 sess = Session()
60 a = sess.dns(name.encode('idna'),'TXT')
61 if a: return b''.join(a[0])
62 return None
63
64
65
66 try:
67 import dns.resolver
68 _get_txt = get_txt_dnspython
69 except ImportError:
70 try:
71 import DNS
72 DNS.DiscoverNameServers()
73 _get_txt = get_txt_pydns
74 except:
75 raise
76
77
79 """Return a TXT record associated with a DNS name.
80
81 @param name: The bytestring domain name to look up.
82 """
83
84 try:
85 unicode_name = name.decode('UTF-8')
86 except UnicodeDecodeError:
87 return None
88 txt = _get_txt(unicode_name, timeout)
89 if type(txt) is str:
90 txt = txt.encode('utf-8')
91 return txt
92