我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用idna.IDNAError()。
def normalize_name(name): ''' Clean the fully qualified name, as defined in ENS `EIP-137 <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_ This does *not* enforce whether ``name`` is a label or fully qualified domain. :param str name: the dot-separated ENS name :raises InvalidName: if ``name`` has invalid syntax ''' if not name: return name elif isinstance(name, (bytes, bytearray)): name = name.decode('utf-8') try: return idna.decode(name, uts46=True, std3_rules=True) except idna.IDNAError as exc: raise InvalidName("%s is an invalid name, because %s" % (name, exc)) from exc
def _get_idna_encoded_host(host): import idna try: host = idna.encode(host, uts46=True).decode('utf-8') except idna.IDNAError: raise UnicodeError return host
def _get_idna_encoded_host(host): try: from .packages import idna except ImportError: # tolerate the possibility of downstream repackagers unvendoring `requests` # For more information, read: packages/__init__.py import idna sys.modules['requests.packages.idna'] = idna try: host = idna.encode(host, uts46=True).decode('utf-8') except idna.IDNAError: raise UnicodeError return host
def ace_address(self): if not is_pure_ascii(self.mailbox): raise ValueError('address {} has no ASCII-compatable encoding' .format(self.address.encode('utf-8'))) ace_hostname = self.hostname if not is_pure_ascii(self.hostname): try: ace_hostname = idna.encode(self.hostname) except idna.IDNAError: raise ValueError('address {} has no ASCII-compatable encoding' .format(self.address.encode('utf-8'))) return '{}@{}'.format(self.mailbox, ace_hostname)
def requires_non_ascii(self): """ Can the address be converted to an ASCII compatible encoding? """ if not is_pure_ascii(self.mailbox): return True if not is_pure_ascii(self.hostname): try: idna.encode(self.hostname) except idna.IDNAError: return True return False
def _lift_parse_result(parse_rs): if isinstance(parse_rs, Mailbox): try: return EmailAddress( display_name=smart_unquote(parse_rs.display_name.decode('utf-8')), mailbox=parse_rs.local_part.decode('utf-8'), hostname=parse_rs.domain.decode('utf-8')) except (UnicodeError, IDNAError): return None if isinstance(parse_rs, Url): return UrlAddress(address=parse_rs.address.decode('utf-8')) return None
def encode(self, label): if label == '': return b'' if self.allow_pure_ascii and self.is_all_ascii(label): return label.encode('ascii') if not have_idna_2008: raise NoIDNA2008 try: if self.uts_46: label = idna.uts46_remap(label, False, self.transitional) return idna.alabel(label) except idna.IDNAError as e: raise IDNAException(idna_exception=e)
def decode(self, label): if not self.strict_decode: return super(IDNA2008Codec, self).decode(label) if label == b'': return u'' if not have_idna_2008: raise NoIDNA2008 try: if self.uts_46: label = idna.uts46_remap(label, False, False) return _escapify(idna.ulabel(label), True) except idna.IDNAError as e: raise IDNAException(idna_exception=e)
def get_surt_host(url): try: host = urlparse(url).hostname except: # self.get_logger().debug("Failed to parse URL {}: {}".format(url, e)) return None if host is None or ExtractHostLinksJob.ip_pattern.match(host): return None host = host.strip().lower() if len(host) < 1 or len(host) > 253: return None parts = host.split('.') if parts[-1] == '': # trailing dot is allowed, strip it parts = parts[0:-1] if parts[0] == 'www' and len(parts) > 1: # strip leading 'www' to reduce number of "duplicate" hosts parts = parts[1:] for i in range(0, len(parts)): part = parts[i] if not ExtractHostLinksJob.host_part_pattern.match(part): try: idn = idna.encode(part).decode('ascii') except (idna.IDNAError, UnicodeDecodeError, IndexError, UnicodeEncodeError, Exception): # self.get_logger().debug("Invalid host name: {}".format(url)) return None if ExtractHostLinksJob.host_part_pattern.match(idn): parts[i] = idn else: # self.get_logger().debug("Invalid host name: {}".format(url)) return None parts.reverse() return '.'.join(parts)
def nameprep(name): if not name: return name try: return idna.decode(name, uts46=True, std3_rules=True) except idna.IDNAError as exc: raise InvalidName("%s is an invalid name, because %s" % (name, exc)) from exc
def decode(self, label): if label == b'': return u'' if not have_idna_2008: raise NoIDNA2008 try: if self.uts_46: label = idna.uts46_remap(label, False, False) return _escapify(idna.ulabel(label), True) except idna.IDNAError as e: raise IDNAException(idna_exception=e)