Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to url-safe encode a string with python? and urllib.quote is wrong

Hello i was wondering if you know any other way to encode a string to a url-safe, because urllib.quote is doing it wrong, the output is different than expected:

If i try

urllib.quote('á')

i get

'%C3%A1'

But thats not the correct output, it should be %E1

As demostrated by the tool provided here this site

And this is not me being difficult, the incorrect output of quote is preventing the browser to found resources, if i try

urllib.quote('\images\á\some file.jpg')

And then i try with the javascript tool i mentioned i get this strings respectively

%5Cimages%5C%C3%A1%5Csome%20file.jpg

%5Cimages%5C%E1%5Csome%20file.jpg

Note how is almost the same but the url provided by quote doesn't work and the other one it does. I tried messing with encode('utf-8) on the string provided to quote but it does not make a difference. I tried with other spanish words with accents and the ñ they all are differently represented.

Is this a python bug? Do you know some module that get this right?

like image 891
Guillermo Siliceo Trueba Avatar asked Dec 09 '25 02:12

Guillermo Siliceo Trueba


1 Answers

According to RFC 3986, %C3%A1 is correct. Characters are supposed to be converted to an octet stream using UTF-8 before the octet stream is percent-encoded. The site you link is out of date.

See Why does the encoding's of a URL and the query string part differ? for more detail on the history of handling non-ASCII characters in URLs.

like image 197
Anomie Avatar answered Dec 10 '25 16:12

Anomie