(url, forceQuote=False)
return urlparse.urlunsplit([parts.scheme, netloc, path, query, parts.fragment])
Attempts to make a unicode URL usuable with ``urllib/urllib2``.

    More specifically, it attempts to convert the unicode object ``url``,
    which is meant to represent a IRI, to an unicode object that,
    containing only ASCII characters, is a valid URI. This involves:

        * IDNA/Puny-encoding the domain name.
        * UTF8-quoting the path and querystring parts.

    See also RFC 3987.

    Reference: http://blog.elsdoerfer.name/2008/12/12/opening-iris-in-python/

    >>> asciifyUrl(u'http://www.\u0161u\u0107uraj.com')
    u'http://www.xn--uuraj-gxa24d.com'
Attempts to make a unicode URL usuable with ``urllib/urllib2``.
Attempts to make a unicode URL usuable with urllib / urllib2 .
def asciifyUrl(url, forceQuote=False):
    """
    Attempts to make a unicode URL usuable with ``urllib/urllib2``.

    More specifically, it attempts to convert the unicode object ``url``,
    which is meant to represent a IRI, to an unicode object that,
    containing only ASCII characters, is a valid URI. This involves:

        * IDNA/Puny-encoding the domain name.
        * UTF8-quoting the path and querystring parts.

    See also RFC 3987.

    Reference: http://blog.elsdoerfer.name/2008/12/12/opening-iris-in-python/

    >>> asciifyUrl(u'http://www.\u0161u\u0107uraj.com')
    u'http://www.xn--uuraj-gxa24d.com'
    """

    parts = urlparse.urlsplit(url)
    if not parts.scheme or not parts.netloc:
        # apparently not an url
        return url

    if all(char in string.printable for char in url):
        return url

    # idna-encode domain
    try:
        hostname = parts.hostname.encode("idna")
    except LookupError:
        hostname = parts.hostname.encode(UNICODE_ENCODING)

    # UTF8-quote the other parts. We check each part individually if
    # if needs to be quoted - that should catch some additional user
    # errors, say for example an umlaut in the username even though
    # the path *is* already quoted.
    def quote(s, safe):
        s = s or ''
        # Triggers on non-ascii characters - another option would be:
        #     urllib.quote(s.replace('%', '')) != s.replace('%', '')
        # which would trigger on all %-characters, e.g. "&".
        if s.encode("ascii", "replace") != s or forceQuote:
            return urllib.quote(s.encode(UNICODE_ENCODING), safe=safe)
        return s

    username = quote(parts.username, '')
    password = quote(parts.password, safe='')
    path = quote(parts.path, safe='/')
    query = quote(parts.query, safe="&=")

    # put everything back together
    netloc = hostname
    if username or password:
        netloc = '@' + netloc
        if password:
            netloc = ':' + password + netloc
        netloc = username + netloc

    try:
        port = parts.port
    except:
        port = None

    if port:
        netloc += ':' + str(port)

    return urlparse.urlunsplit([parts.scheme, netloc, path, query, parts.fragment])
def asciifyUrl ( url , forceQuote = False ) : parts = urlparse . urlsplit ( url ) if not parts . scheme or not parts . netloc : # apparently not an url return url if all ( char in string . printable for char in url ) : return url # idna-encode domain try : hostname = parts . hostname . encode ( "idna" ) except LookupError : hostname = parts . hostname . encode ( UNICODE_ENCODING ) # UTF8-quote the other parts. We check each part individually if # if needs to be quoted - that should catch some additional user # errors, say for example an umlaut in the username even though # the path *is* already quoted. def quote ( s , safe ) : s = s or '' # Triggers on non-ascii characters - another option would be: #     urllib.quote(s.replace('%', '')) != s.replace('%', '') # which would trigger on all %-characters, e.g. "&". if s . encode ( "ascii" , "replace" ) != s or forceQuote : return urllib . quote ( s . encode ( UNICODE_ENCODING ) , safe = safe ) return s username = quote ( parts . username , '' ) password = quote ( parts . password , safe = '' ) path = quote ( parts . path , safe = '/' ) query = quote ( parts . query , safe = "&=" ) # put everything back together netloc = hostname if username or password : netloc = '@' + netloc if password : netloc = ':' + password + netloc netloc = username + netloc try : port = parts . port except : port = None if port : netloc += ':' + str ( port ) return urlparse . urlunsplit ( [ parts . scheme , netloc , path , query , parts . fragment ] )