def isInvalid(self, value): if not value: return "No value entered" try: assert len(value) < 256 account, domain = value.split(u"@") subDomain, tld = domain.rsplit(".", 1) assert account and subDomain and tld assert subDomain[0] != "." assert len(account) <= 64 except: return "Invalid email entered" isValid = True validChars = string.ascii_letters + string.digits + "!#$%&'*+-/=?^_`{|}~." unicodeLowerBound = u"\u0080" unicodeUpperBound = u"\U0010FFFF" for char in account: if not (char in validChars or (char >= unicodeLowerBound and char <= unicodeUpperBound)): isValid = False try: idna.ToASCII(subDomain) idna.ToASCII(tld) except: isValid = False if " " in subDomain or " " in tld: isValid = False if isValid: return None else: return "Invalid email entered"
def are_domains_equal(a,b): """Compare two International Domain Names. :Parameters: - `a`,`b`: domains names to compare :return: True `a` and `b` are equal as domain names.""" a=idna.ToASCII(a) b=idna.ToASCII(b) return a.lower()==b.lower()
def EncodeDNS(name): out = [] for part in name.split('.'): if len(part) == 0: continue out.append(idna.ToASCII(part)) return '.'.join(out)
def resolve_srv(domain, service, proto="tcp"): """Resolve service domain to server name and port number using SRV records. A built-in service alias table will be used to lookup also some obsolete record names. :Parameters: - `domain`: domain name. - `service`: service name. - `proto`: protocol name. :Types: - `domain`: `unicode` or `str` - `service`: `unicode` or `str` - `proto`: `str` :return: host names and port numbers for the service or None. :returntype: `list` of (`str`,`int`)""" names_to_try = [u"_%s._%s.%s" % (service, proto, domain)] if service_aliases.has_key(service): for a in service_aliases[service]: names_to_try.append(u"_%s._%s.%s" % (a, proto, domain)) for name in names_to_try: name = idna.ToASCII(name) try: r = dns.resolver.query(name, 'SRV') except dns.exception.DNSException: continue if not r: continue return [(rr.target.to_text(), rr.port) for rr in reorder_srv(r)] return None
def safe_localhost(): # RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and # if that can't be calculated, that we should use a domain literal # instead (essentially an encoded IP address like [A.B.C.D]). try: fqdn = decode_fqdn(socket.getfqdn()) except UnicodeDecodeError: if not iswindows: raise from calibre_extensions.winutil import get_computer_name fqdn = get_computer_name() if '.' in fqdn and fqdn != '.': # Some mail servers have problems with non-ascii local hostnames, see # https://bugs.launchpad.net/bugs/1256549 try: local_hostname = as_unicode(idna.ToASCII(fqdn)) except Exception: local_hostname = 'localhost.localdomain' else: # We can't find an fqdn hostname, so use a domain literal addr = '127.0.0.1' try: addr = socket.gethostbyname(socket.gethostname()) except socket.gaierror: pass local_hostname = '[%s]' % addr return local_hostname
def idn_to_ascii(host): '''convert IDN (Internationalized Domain Names) to ACE (ASCII-compatible encoding)''' labels = idna.dots.split(host) converted_labels = [] for label in labels: converted_labels.append(idna.ToASCII(label)) return ".".join(converted_labels)
def CreateRR(name): out = [] for part in name.split('.'): if len(part) == 0: continue part_ascii = idna.ToASCII(part) out.append(chr(len(part_ascii))) out.append(part_ascii) out.append('\0') return ''.join(out)
def gettld(bot, trigger): """Show information about the given Top Level Domain.""" tld = trigger.group(2) if not tld: bot.reply("You must provide a top-level domain to search.") return # Stop if no tld argument is provided tld = tld.strip('.').lower() if not bot.memory['tld_list_cache']: _update_tld_data(bot, 'list') tld_list = bot.memory['tld_list_cache'] if not any([ name in tld_list for name in [tld, idna.ToASCII(tld).decode('utf-8')] ]): bot.reply( "The top-level domain '{}' is not in IANA's list of valid TLDs." .format(tld)) return if not bot.memory['tld_data_cache']: _update_tld_data(bot, 'data') tld_data = bot.memory['tld_data_cache'] record = tld_data.get(tld, None) if not record: bot.say( "The top-level domain '{}' exists, " "but no details about it could be found." .format(tld) ) return # Get the current order of available data fields fields = list(record.keys()) # This trick moves matching keys to the end of the list fields.sort(key=lambda s: s.startswith('Notes') or s.startswith('Comments')) items = [] for field in fields: value = record[field] if value: items.append('{}: {}'.format(field, value)) message = ' | '.join(items) usable, excess = tools.get_sendable_message(message) if excess: message = usable + ' […]' bot.say(message)
def getaddrinfo(host, port, family=0, socktype=socket.SOCK_STREAM, proto=0, allow_cname=True): """Resolve host and port into addrinfo struct. Does the same thing as socket.getaddrinfo, but using `pyxmpp.resolver`. This makes it possible to reuse data (A records from the additional section of DNS reply) returned with SRV records lookup done using this module. :Parameters: - `host`: service domain name. - `port`: service port number or name. - `family`: address family. - `socktype`: socket type. - `proto`: protocol number or name. - `allow_cname`: when False CNAME responses are not allowed. :Types: - `host`: `unicode` or `str` - `port`: `int` or `str` - `family`: `int` - `socktype`: `int` - `proto`: `int` or `str` - `allow_cname`: `bool` :return: list of (family, socktype, proto, canonname, sockaddr). :returntype: `list` of (`int`, `int`, `int`, `str`, (`str`, `int`))""" ret = [] if proto == 0: proto = socket.getprotobyname("tcp") elif type(proto) != int: proto = socket.getprotobyname(proto) if type(port) != int: port = socket.getservbyname(port, proto) if family not in (0, socket.AF_INET): raise NotImplementedError, "Protocol family other than AF_INET not supported, yet" if ip_re.match(host): return [(socket.AF_INET, socktype, proto, host, (host, port))] host = idna.ToASCII(host) try: r = dns.resolver.query(host, 'A') except dns.exception.DNSException: r = dns.resolver.query(host + ".", 'A') if not allow_cname and r.rrset.name != dns.name.from_text(host): raise ValueError, "Unexpected CNAME record found for %s" % (host, ) if r: for rr in r: ret.append((socket.AF_INET, socktype, proto, r.rrset.name, (rr.to_text(), port))) return ret
def punycode_special_host(url): if url.host and url.scheme in urlcanon.SPECIAL_SCHEMES: # https://github.com/kjd/idna/issues/40#issuecomment-285496926 try: url.host = idna.encode(url.host.decode('utf-8'), uts46=True) except: try: remapped = idna.uts46_remap(url.host.decode('utf-8')) labels = remapped.split('.') punycode_labels = [idna2003.ToASCII(label) for label in labels] url.host = b'.'.join(punycode_labels) except: pass
def idn_to_ascii(host): """ Convert IDN (Internationalized Domain Names) to ACE (ASCII-compatible encoding) """ from encodings import idna labels = idna.dots.split(host) converted_labels = [] for label in labels: if label: converted_labels.append(idna.ToASCII(label).decode('utf-8')) else: converted_labels.append('') return ".".join(converted_labels)
def _parse_labels(string): try: # Note: idna.ToASCII also enforces the minimum and maximum label length. labels = tuple(idna.ToASCII(x).lower() for x in string.split(u".")) except UnicodeError: return None if len(labels) + sum(len(x) for x in labels) > 253: return None if labels[-1].isdigit(): return None if not all(_LABEL_REX.match(x) for x in labels): return None return tuple(idna.ToUnicode(x) for x in labels)
def _domain_to_ascii(domain, strict=False): """Attempt to encode with IDNA 2008 first, if that fails then attempt to encode with IDNA 2003. """ try: return idna.encode(domain, strict=strict, std3_rules=strict, uts46=True, transitional=False) except idna.IDNAError: if isinstance(domain, (bytes, bytearray)): domain = domain.decode("ascii") domain = idna.uts46_remap(domain, std3_rules=strict, transitional=False) trailing_dot = False result = [] if strict: labels = domain.split(".") else: labels = IDNA_DOTS_REGEX.split(domain) if not labels or labels == [""]: raise idna.IDNAError("Empty domain") if labels[-1] == "": del labels[-1] trailing_dot = True for label in labels: try: s = idna2003.ToASCII(label) except UnicodeError: if strict: raise result.append(label.encode("utf-8")) continue if s: result.append(s) else: raise idna.IDNAError("Empty label") if trailing_dot: result.append(b"") s = b".".join(result) if not idna.valid_string_length(s, trailing_dot): raise idna.IDNAError("Domain too long") return s
def safe_localhost(): # RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and # if that can't be calculated, that we should use a domain literal # instead (essentially an encoded IP address like [A.B.C.D]). fqdn = socket.getfqdn() if '.' in fqdn: # Some mail servers have problems with non-ascii local hostnames, see # https://bugs.launchpad.net/bugs/1256549 try: local_hostname = idna.ToASCII(force_unicode(fqdn)) except: local_hostname = 'localhost.localdomain' else: # We can't find an fqdn hostname, so use a domain literal addr = '127.0.0.1' try: addr = socket.gethostbyname(socket.gethostname()) except socket.gaierror: pass local_hostname = '[%s]' % addr return local_hostname
def prepareIDNName(name): """ Encode a unicode IDN Domain Name into its ACE equivalent. This will encode the domain labels, separated by allowed dot code points, to their ASCII Compatible Encoding (ACE) equivalent, using punycode. The result is an ASCII byte string of the encoded labels, separated by the standard full stop. """ result = [] labels = idna.dots.split(name) if labels and len(labels[-1]) == 0: trailing_dot = b'.' del labels[-1] else: trailing_dot = b'' for label in labels: result.append(idna.ToASCII(label)) return b'.'.join(result) + trailing_dot
def __init__(self, url, ssl_verify_cert=True, ssl_ca_certs=None): self.ssl_verify_cert = ssl_verify_cert self.ssl_ca_certs = ssl_ca_certs scheme, host, path, _, _, _ = urlparse.urlparse(url) if isinstance(host, unicode): host = idna.ToASCII(host) self.host = host if isinstance(path, unicode): path = path.encode("utf-8") self.path = urllib.quote(path) + "?action=xmlrpc2" self.headers = dict(Connection="Keep-Alive") self.creds = None if scheme.strip().lower() == "http": self.connection = HTTPConnection(self.host) else: self.connection = HTTPSConnection(self.host, verify_cert=self.ssl_verify_cert, ca_certs=self.ssl_ca_certs) self.connection.connect()
try: addr = _validate_ip_address(socket.AF_INET, data) except ValueError, err: logger.debug("ValueError: {0}".format(err)) data = UNICODE_DOT_RE.sub(u".", data) data = data.rstrip(u".") labels = data.split(u".") try: labels = [idna.nameprep(label) for label in labels] except UnicodeError: raise JIDError(u"Domain name invalid") for label in labels: if not STD3_LABEL_RE.match(label): raise JIDError(u"Domain name invalid") try: idna.ToASCII(label) except UnicodeError: raise JIDError(u"Domain name invalid") domain = u".".join(labels) if len(domain.encode("utf-8")) > 1023: raise JIDError(u"Domain name too long") return domain @staticmethod def __prepare_resource(data): """Prepare the resourcepart of the JID. :Parameters: - `data`: Resourcepart of the JID :raise JIDError: if the resource name is too long.
def encode(host, uts46=False): # pylint: disable=unused-argument # Used by urllib3 return idna.ToASCII(host)