Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
e164.py117 linesDownload Raw Back to dns
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license2 3# Copyright (C) 2006-2017 Nominum, Inc.4#5# Permission to use, copy, modify, and distribute this software and its6# documentation for any purpose with or without fee is hereby granted,7# provided that the above copyright notice and this permission notice8# appear in all copies.9#10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17 18"""DNS E.164 helpers."""19 20from typing import Iterable, Optional, Union21 22import dns.exception23import dns.name24import dns.resolver25 26#: The public E.164 domain.27public_enum_domain = dns.name.from_text("e164.arpa.")28 29 30def from_e164(31    text: str, origin: Optional[dns.name.Name] = public_enum_domain32) -> dns.name.Name:33    """Convert an E.164 number in textual form into a Name object whose34    value is the ENUM domain name for that number.35 36    Non-digits in the text are ignored, i.e. "16505551212",37    "+1.650.555.1212" and "1 (650) 555-1212" are all the same.38 39    *text*, a ``str``, is an E.164 number in textual form.40 41    *origin*, a ``dns.name.Name``, the domain in which the number42    should be constructed.  The default is ``e164.arpa.``.43 44    Returns a ``dns.name.Name``.45    """46 47    parts = [d for d in text if d.isdigit()]48    parts.reverse()49    return dns.name.from_text(".".join(parts), origin=origin)50 51 52def to_e164(53    name: dns.name.Name,54    origin: Optional[dns.name.Name] = public_enum_domain,55    want_plus_prefix: bool = True,56) -> str:57    """Convert an ENUM domain name into an E.164 number.58 59    Note that dnspython does not have any information about preferred60    number formats within national numbering plans, so all numbers are61    emitted as a simple string of digits, prefixed by a '+' (unless62    *want_plus_prefix* is ``False``).63 64    *name* is a ``dns.name.Name``, the ENUM domain name.65 66    *origin* is a ``dns.name.Name``, a domain containing the ENUM67    domain name.  The name is relativized to this domain before being68    converted to text.  If ``None``, no relativization is done.69 70    *want_plus_prefix* is a ``bool``.  If True, add a '+' to the beginning of71    the returned number.72 73    Returns a ``str``.74 75    """76    if origin is not None:77        name = name.relativize(origin)78    dlabels = [d for d in name.labels if d.isdigit() and len(d) == 1]79    if len(dlabels) != len(name.labels):80        raise dns.exception.SyntaxError("non-digit labels in ENUM domain name")81    dlabels.reverse()82    text = b"".join(dlabels)83    if want_plus_prefix:84        text = b"+" + text85    return text.decode()86 87 88def query(89    number: str,90    domains: Iterable[Union[dns.name.Name, str]],91    resolver: Optional[dns.resolver.Resolver] = None,92) -> dns.resolver.Answer:93    """Look for NAPTR RRs for the specified number in the specified domains.94 95    e.g. lookup('16505551212', ['e164.dnspython.org.', 'e164.arpa.'])96 97    *number*, a ``str`` is the number to look for.98 99    *domains* is an iterable containing ``dns.name.Name`` values.100 101    *resolver*, a ``dns.resolver.Resolver``, is the resolver to use.  If102    ``None``, the default resolver is used.103    """104 105    if resolver is None:106        resolver = dns.resolver.get_default_resolver()107    e_nx = dns.resolver.NXDOMAIN()108    for domain in domains:109        if isinstance(domain, str):110            domain = dns.name.from_text(domain)111        qname = dns.e164.from_e164(number, domain)112        try:113            return resolver.resolve(qname, "NAPTR")114        except dns.resolver.NXDOMAIN as e:115            e_nx += e116    raise e_nx117 
codekingpro/portable-devtools · Team Ai