If I do
url = "http://example.com?p=" + urllib.quote(query)
/
to %2F
(breaks OAuth normalization)Is there a better library?
The quote() function encodes space characters to %20 . If you want to encode space characters to plus sign ( + ), then you can use another function named quote_plus provided by urllib.
URL Encoding (Percent Encoding)URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
In Python, we can URL encode a query string using the urlib. parse module, which further contains a function urlencode() for encoding the query string in URL. The query string is simply a string of key-value pairs.
From the documentation:
urllib.quote(string[, safe])
Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of the URL.The optional safe parameter specifies additional characters that should not be quoted — its default value is '/'
That means passing ''
for safe will solve your first issue:
>>> urllib.quote('/test') '/test' >>> urllib.quote('/test', safe='') '%2Ftest'
About the second issue, there is a bug report about it. Apparently it was fixed in Python 3. You can workaround it by encoding as UTF-8 like this:
>>> query = urllib.quote(u"Müller".encode('utf8')) >>> print urllib.unquote(query).decode('utf8') Müller
By the way, have a look at urlencode.
In Python 3, the function quote
has been moved to urllib.parse
:
>>> import urllib.parse >>> print(urllib.parse.quote("Müller".encode('utf8'))) M%C3%BCller >>> print(urllib.parse.unquote("M%C3%BCller")) Müller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With