ietfparse/algorithms.py
def _normalize_host(host, enable_long_host=False, encode_with_idna=None,
                    scheme=None):
    """
    Normalize a host for a URL.

    :param str host: the host name to normalize

    :keyword bool enable_long_host: if this keyword is specified
        and it is :data:`True`, then the host name length restriction
        from :rfc:`3986#section-3.2.2` is relaxed.
    :keyword bool encode_with_idna: if this keyword is specified
        and it is :data:`True`, then the ``host`` parameter will be
        encoded using IDN.  If this value is provided as :data:`False`,
        then the percent-encoding scheme is used instead.  If this
        parameter is omitted or included with a different value, then
        the ``host`` parameter is processed using :data:`IDNA_SCHEMES`.
    :keyword str scheme: if this keyword is specified, then it is
        used to determine whether to apply IDN rules or not.  This
        parameter is ignored if `encode_with_idna` is not :data:`None`.

    :return: the normalized and encoded string ready for inclusion
        into a URL

    """
    if encode_with_idna is not None:
        enable_idna = encode_with_idna
    else:
        enable_idna = scheme.lower() in IDNA_SCHEMES if scheme else False
    if enable_idna:
        try:
            host = '.'.join(segment.encode('idna').decode()
                            for segment in host.split('.'))
        except UnicodeError as exc:
            raise ValueError('host is invalid - {0}'.format(exc))
    else:
        host = parse.quote(host.encode('utf-8'), safe=HOST_SAFE_CHARS)

    if len(host) > 255 and not enable_long_host:
        raise ValueError('host too long')

    return host
def _normalize_host(host, enable_long_host=False, encode_with_idna=None,
                    scheme=None):
    if encode_with_idna is not None:
        enable_idna = encode_with_idna
    else:
        enable_idna = scheme.lower() in IDNA_SCHEMES if scheme else False
    if enable_idna:
        try:
            host = '.'.join(segment.encode('idna').decode()
                            for segment in host.split('.'))
        except UnicodeError as exc:
            raise ValueError('host is invalid - {0}'.format(exc))
    else:
        host = parse.quote(host.encode('utf-8'), safe=HOST_SAFE_CHARS)

    if len(host) > 255 and not enable_long_host:
        raise ValueError('host too long')

    return host
def _normalize_host ( host , enable_long_host = False , encode_with_idna = None , scheme = None ) : if encode_with_idna is not None : enable_idna = encode_with_idna else : enable_idna = scheme . lower ( ) in IDNA_SCHEMES if scheme else False if enable_idna : try : host = '.' . join ( segment . encode ( 'idna' ) . decode ( ) for segment in host . split ( '.' ) ) except UnicodeError as exc : raise ValueError ( 'host is invalid - {0}' . format ( exc ) ) else : host = parse . quote ( host . encode ( 'utf-8' ) , safe = HOST_SAFE_CHARS ) if len ( host ) > 255 and not enable_long_host : raise ValueError ( 'host too long' ) return host
Normalize a host for a URL.

    :param str host: the host name to normalize

    :keyword bool enable_long_host: if this keyword is specified
        and it is :data:`True`, then the host name length restriction
        from :rfc:`3986#section-3.2.2` is relaxed.
    :keyword bool encode_with_idna: if this keyword is specified
        and it is :data:`True`, then the ``host`` parameter will be
        encoded using IDN.  If this value is provided as :data:`False`,
        then the percent-encoding scheme is used instead.  If this
        parameter is omitted or included with a different value, then
        the ``host`` parameter is processed using :data:`IDNA_SCHEMES`.
    :keyword str scheme: if this keyword is specified, then it is
        used to determine whether to apply IDN rules or not.  This
        parameter is ignored if `encode_with_idna` is not :data:`None`.

    :return: the normalized and encoded string ready for inclusion
        into a URL
Normalize a host for a URL .