Package dns :: Module e164
[hide private]
[frames] | no frames]

Source Code for Module dns.e164

 1  # Copyright (C) 2006, 2007, 2009, 2011 Nominum, Inc. 
 2  # 
 3  # Permission to use, copy, modify, and distribute this software and its 
 4  # documentation for any purpose with or without fee is hereby granted, 
 5  # provided that the above copyright notice and this permission notice 
 6  # appear in all copies. 
 7  # 
 8  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 9  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
10  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
11  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
12  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
13  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
14  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
15   
16  """DNS E.164 helpers 
17   
18  @var public_enum_domain: The DNS public ENUM domain, e164.arpa. 
19  @type public_enum_domain: dns.name.Name object 
20  """ 
21   
22   
23  import dns.exception 
24  import dns.name 
25  import dns.resolver 
26  from ._compat import string_types 
27   
28  public_enum_domain = dns.name.from_text('e164.arpa.') 
29   
30   
31 -def from_e164(text, origin=public_enum_domain):
32 """Convert an E.164 number in textual form into a Name object whose 33 value is the ENUM domain name for that number. 34 @param text: an E.164 number in textual form. 35 @type text: str 36 @param origin: The domain in which the number should be constructed. 37 The default is e164.arpa. 38 @type origin: dns.name.Name object or None 39 @rtype: dns.name.Name object 40 """ 41 parts = [d for d in text if d.isdigit()] 42 parts.reverse() 43 return dns.name.from_text('.'.join(parts), origin=origin)
44 45
46 -def to_e164(name, origin=public_enum_domain, want_plus_prefix=True):
47 """Convert an ENUM domain name into an E.164 number. 48 @param name: the ENUM domain name. 49 @type name: dns.name.Name object. 50 @param origin: A domain containing the ENUM domain name. The 51 name is relativized to this domain before being converted to text. 52 @type origin: dns.name.Name object or None 53 @param want_plus_prefix: if True, add a '+' to the beginning of the 54 returned number. 55 @rtype: str 56 """ 57 if origin is not None: 58 name = name.relativize(origin) 59 dlabels = [d for d in name.labels if d.isdigit() and len(d) == 1] 60 if len(dlabels) != len(name.labels): 61 raise dns.exception.SyntaxError('non-digit labels in ENUM domain name') 62 dlabels.reverse() 63 text = b''.join(dlabels) 64 if want_plus_prefix: 65 text = b'+' + text 66 return text
67 68
69 -def query(number, domains, resolver=None):
70 """Look for NAPTR RRs for the specified number in the specified domains. 71 72 e.g. lookup('16505551212', ['e164.dnspython.org.', 'e164.arpa.']) 73 """ 74 if resolver is None: 75 resolver = dns.resolver.get_default_resolver() 76 e_nx = dns.resolver.NXDOMAIN() 77 for domain in domains: 78 if isinstance(domain, string_types): 79 domain = dns.name.from_text(domain) 80 qname = dns.e164.from_e164(number, domain) 81 try: 82 return resolver.query(qname, 'NAPTR') 83 except dns.resolver.NXDOMAIN as e: 84 e_nx += e 85 raise e_nx
86