Package dkim :: Module dnsplug
[hide private]
[frames] | no frames]

Source Code for Module dkim.dnsplug

 1  # This software is provided 'as-is', without any express or implied 
 2  # warranty.  In no event will the author be held liable for any damages 
 3  # arising from the use of this software. 
 4  # 
 5  # Permission is granted to anyone to use this software for any purpose, 
 6  # including commercial applications, and to alter it and redistribute it 
 7  # freely, subject to the following restrictions: 
 8  # 
 9  # 1. The origin of this software must not be misrepresented; you must not 
10  #    claim that you wrote the original software. If you use this software 
11  #    in a product, an acknowledgment in the product documentation would be 
12  #    appreciated but is not required. 
13  # 2. Altered source versions must be plainly marked as such, and must not be 
14  #    misrepresented as being the original software. 
15  # 3. This notice may not be removed or altered from any source distribution. 
16  # 
17  # Copyright (c) 2008 Greg Hewgill http://hewgill.com 
18  # 
19  # This has been modified from the original software. 
20  # Copyright (c) 2011 William Grant <me@williamgrant.id.au> 
21   
22   
23  __all__ = [ 
24      'get_txt' 
25      ] 
26   
27   
28 -def get_txt_dnspython(name, timeout=5):
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
39 -def get_txt_pydns(name, timeout=5):
40 """Return a TXT record associated with a DNS name.""" 41 # Older pydns releases don't like a trailing dot. 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 # No longer used since it doesn't support timeout
54 -def get_txt_Milter_dns(name, timeout=5):
55 """Return a TXT record associated with a DNS name.""" 56 # Older pydns releases don't like a trailing dot. 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 # Prefer dnspython if it's there, otherwise use pydns. 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
78 -def get_txt(name, timeout=5):
79 """Return a TXT record associated with a DNS name. 80 81 @param name: The bytestring domain name to look up. 82 """ 83 # pydns needs Unicode, but DKIM's d= is ASCII (already punycoded). 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