def ascii_to_idn(host): '''convert ACE (ASCII-compatible encoding) to IDN (Internationalized Domain Names)''' labels = idna.dots.split(host) converted_labels = [] for label in labels: converted_labels.append(idna.ToUnicode(label)) return ".".join(converted_labels)
def idn_to_unicode(s): rv = [] for part in s.split('.'): if part.startswith('xn--'): rv.append(idna.ToUnicode(part)) else: rv.append(part) return '.'.join(rv)
def norm(self, valu, oldval=None): valu = valu.replace('[.]', '.') if not fqdnre.match(valu): self._raiseBadValu(valu) if valu.startswith('xn--'): valu = idna.ToUnicode(valu) valu = valu.lower() parts = valu.split('.', 1) subs = {'host': parts[0]} if len(parts) == 2: subs['domain'] = parts[1] else: subs['sfx'] = 1 return valu, subs
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 mightDecode(s): if s[:4] == "xn--": return idna.ToUnicode(s) return s
def decode(host): # Used by cryptography/hazmat/backends/openssl/x509.py return idna.ToUnicode(host)