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)
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').req()
45 if not response.answers:
46 return None
47 return b''.join(response.answers[0]['data'])
48
50 """Return a TXT record associated with a DNS name."""
51
52 if name.endswith('.'):
53 name = name[:-1]
54 sess = Session()
55 a = sess.dns(name,'TXT')
56 if a: return b''.join(a[0])
57 return None
58
59
60 try:
61 import dns.resolver
62 _get_txt = get_txt_dnspython
63 except ImportError:
64 try:
65 from Milter.dns import Session
66 _get_txt = get_txt_Milter_dns
67 except ImportError:
68 import DNS
69 DNS.DiscoverNameServers()
70 _get_txt = get_txt_pydns
71
73 """Return a TXT record associated with a DNS name.
74
75 @param name: The bytestring domain name to look up.
76 """
77
78 try:
79 unicode_name = name.decode('ascii')
80 except UnicodeDecodeError:
81 return None
82 txt = _get_txt(unicode_name)
83 if type(txt) is str:
84 txt = txt.encode('utf-8')
85 return txt
86