def iri_to_uri(value, normalize=False):
    """
    Encodes a unicode IRI into an ASCII byte string URI

    :param value:
        A unicode string of an IRI

    :param normalize:
        A bool that controls URI normalization

    :return:
        A byte string of the ASCII-encoded URI
    """

    if not isinstance(value, str_cls):
        raise TypeError(unwrap(
            '''
            value must be a unicode string, not %s
            ''',
            type_name(value)
        ))

    scheme = None
    # Python 2.6 doesn't split properly is the URL doesn't start with http:// or https://
    if sys.version_info < (2, 7) and not value.startswith('http://') and not value.startswith('https://'):
        real_prefix = None
        prefix_match = re.match('^[^:]*://', value)
        if prefix_match:
            real_prefix = prefix_match.group(0)
            value = 'http://' + value[len(real_prefix):]
        parsed = urlsplit(value)
        if real_prefix:
            value = real_prefix + value[7:]
            scheme = _urlquote(real_prefix[:-3])
    else:
        parsed = urlsplit(value)

    if scheme is None:
        scheme = _urlquote(parsed.scheme)
    hostname = parsed.hostname
    if hostname is not None:
        hostname = hostname.encode('idna')
    # RFC 3986 allows userinfo to contain sub-delims
    username = _urlquote(parsed.username, safe='!$&\'()*+,;=')
    password = _urlquote(parsed.password, safe='!$&\'()*+,;=')
    port = parsed.port
    if port is not None:
        port = str_cls(port).encode('ascii')

    netloc = b''
    if username is not None:
        netloc += username
        if password:
            netloc += b':' + password
        netloc += b'@'
    if hostname is not None:
        netloc += hostname
    if port is not None:
        default_http = scheme == b'http' and port == b'80'
        default_https = scheme == b'https' and port == b'443'
        if not normalize or (not default_http and not default_https):
            netloc += b':' + port

    # RFC 3986 allows a path to contain sub-delims, plus "@" and ":"
    path = _urlquote(parsed.path, safe='/!$&\'()*+,;=@:')
    # RFC 3986 allows the query to contain sub-delims, plus "@", ":" , "/" and "?"
    query = _urlquote(parsed.query, safe='/?!$&\'()*+,;=@:')
    # RFC 3986 allows the fragment to contain sub-delims, plus "@", ":" , "/" and "?"
    fragment = _urlquote(parsed.fragment, safe='/?!$&\'()*+,;=@:')

    if normalize and query is None and fragment is None and path == b'/':
        path = None

    # Python 2.7 compat
    if path is None:
        path = ''

    output = urlunsplit((scheme, netloc, path, query, fragment))
    if isinstance(output, str_cls):
        output = output.encode('latin1')
    return output
def iri_to_uri ( value , normalize = False ) : if not isinstance ( value , str_cls ) : raise TypeError ( unwrap ( '''
            value must be a unicode string, not %s
            ''' , type_name ( value ) ) ) scheme = None if sys . version_info < ( 2 , 7 ) and not value . startswith ( 'http://' ) and not value . startswith ( 'https://' ) : real_prefix = None prefix_match = re . match ( '^[^:]*://' , value ) if prefix_match : real_prefix = prefix_match . group ( 0 ) value = 'http://' + value [ len ( real_prefix ) : ] parsed = urlsplit ( value ) if real_prefix : value = real_prefix + value [ 7 : ] scheme = _urlquote ( real_prefix [ : - 3 ] ) else : parsed = urlsplit ( value ) if scheme is None : scheme = _urlquote ( parsed . scheme ) hostname = parsed . hostname if hostname is not None : hostname = hostname . encode ( 'idna' ) username = _urlquote ( parsed . username , safe = '!$&\'()*+,;=' ) password = _urlquote ( parsed . password , safe = '!$&\'()*+,;=' ) port = parsed . port if port is not None : port = str_cls ( port ) . encode ( 'ascii' ) netloc = b'' if username is not None : netloc += username if password : netloc += b':' + password netloc += b'@' if hostname is not None : netloc += hostname if port is not None : default_http = scheme == b'http' and port == b'80' default_https = scheme == b'https' and port == b'443' if not normalize or ( not default_http and not default_https ) : netloc += b':' + port path = _urlquote ( parsed . path , safe = '/!$&\'()*+,;=@:' ) query = _urlquote ( parsed . query , safe = '/?!$&\'()*+,;=@:' ) fragment = _urlquote ( parsed . fragment , safe = '/?!$&\'()*+,;=@:' ) if normalize and query is None and fragment is None and path == b'/' : path = None if path is None : path = '' output = urlunsplit ( ( scheme , netloc , path , query , fragment ) ) if isinstance ( output , str_cls ) : output = output . encode ( 'latin1' ) return output
Encodes a unicode IRI into an ASCII byte string URI

    :param value:
        A unicode string of an IRI

    :param normalize:
        A bool that controls URI normalization

    :return:
        A byte string of the ASCII-encoded URI