Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Code:</h2> <pre><code>import urlparse, urllib def fixurl(url): # turn string into unicode if not isinstance(url,unicode): url = url.decode('utf8') # parse it parsed = urlparse.urlsplit(url) # divide the netloc further userpass,at,hostport = parsed.netloc.rpartition('@') user,colon1,pass_ = userpass.partition(':') host,colon2,port = hostport.partition(':') # encode each component scheme = parsed.scheme.encode('utf8') user = urllib.quote(user.encode('utf8')) colon1 = colon1.encode('utf8') pass_ = urllib.quote(pass_.encode('utf8')) at = at.encode('utf8') host = host.encode('idna') colon2 = colon2.encode('utf8') port = port.encode('utf8') path = '/'.join( # could be encoded slashes! urllib.quote(urllib.unquote(pce).encode('utf8'),'') for pce in parsed.path.split('/') ) query = urllib.quote(urllib.unquote(parsed.query).encode('utf8'),'=&amp;?/') fragment = urllib.quote(urllib.unquote(parsed.fragment).encode('utf8')) # put it back together netloc = ''.join((user,colon1,pass_,at,host,colon2,port)) return urlparse.urlunsplit((scheme,netloc,path,query,fragment)) print fixurl('http://\xe2\x9e\xa1.ws/\xe2\x99\xa5') print fixurl('http://\xe2\x9e\xa1.ws/\xe2\x99\xa5/%2F') print fixurl(u'http://Åsa:abc123@➡.ws:81/admin') print fixurl(u'http://➡.ws/admin') </code></pre> <h2>Output:</h2> <blockquote> <p><code>http://xn--hgi.ws/%E2%99%A5</code><br> <code>http://xn--hgi.ws/%E2%99%A5/%2F</code><br> <code>http://%C3%85sa:abc123@xn--hgi.ws:81/admin</code><br> <code>http://xn--hgi.ws/admin</code></p> </blockquote> <h2>Read more:</h2> <ul> <li><a href="http://docs.python.org/library/urllib.html#urllib.quote" rel="nofollow noreferrer">urllib.quote()</a></li> <li><a href="https://docs.python.org/2/library/urlparse.html#urlparse.urlparse" rel="nofollow noreferrer">urlparse.urlparse()</a></li> <li><a href="https://docs.python.org/2/library/urlparse.html#urlparse.urlunparse" rel="nofollow noreferrer">urlparse.urlunparse()</a></li> <li><a href="https://docs.python.org/2/library/urlparse.html#urlparse.urlsplit" rel="nofollow noreferrer">urlparse.urlsplit()</a></li> <li><a href="https://docs.python.org/2/library/urlparse.html#urlparse.urlunsplit" rel="nofollow noreferrer">urlparse.urlunsplit()</a></li> </ul> <h2>Edits:</h2> <ul> <li>Fixed the case of already quoted characters in the string.</li> <li>Changed <code>urlparse</code>/<code>urlunparse</code> to <code>urlsplit</code>/<code>urlunsplit</code>.</li> <li>Don't encode user and port information with the hostname. (Thanks Jehiah)</li> <li>When "@" is missing, don't treat the host/port as user/pass! (Thanks hupf)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload