Urlencode () for RFC 3986

Python has an awesome urlencode () function that encodes dictthrough RFC 1738 (plus encoding):

>>> urllib.parse.urlencode({'site':'Stack Overflow','Coder':'Jeff Atwood'})
'Coder=Jeff+Atwood&site=Stack+Overflow'

I can’t find a replacement that uses RFC 3986 (Percent Encoding), although the thin guide states the following :

RFC 3986 - Uniform Resource Identifiers
This is the current standard (STD66). Any changes to the urllib.parse module should be consistent with this.

This will be the expected result:

>>> urllib.parse.urlencode({'site':'Stack Overflow','Coder':'Jeff Atwood'})
'Coder=Jeff%20Atwood&site=Stack%20Overflow'

Of course, I could collapse myself, but I find it amazing that I cannot find such a built-in Python function. Is there such a Python function that I just can't find?

+4
2

:

def percent_encoding(string):
    result = ''
    accepted = [c for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.encode('utf-8')]
    for char in string.encode('utf-8'):
        result += chr(char) if char in accepted else '%{}'.format(hex(char)[2:]).upper()
    return result

>>> percent_encoding('http://www.google.com')
'http%3A%2F%2Fwww.google.com'

>>> percent_encoding('ñapa')
'%C3%B1apa'

, , , , /, .

def percent_urlencode(dictionary):
    return '&'.join(["{}={}".format(k, percent_encoding(str(v))) for k, v in dictionary.items()])

>>> percent_urlencode({'token': '$%&/', 'username': 'me'})
'username=me&token=%24%25%26%2F'

>>> percent_urlencode({'site':'Stack Overflow','Coder':'Jeff Atwood'})
'site=Stack%20Overflow&Coder=Jeff%20Atwood'
+1

All Articles