urlencoding

A python library for query string parsing and generation.

The code is hosted here at github. The latest code can be downloaded as a zip file or a tarball.

Requires Python 2.6 or newer.

Can be installed using pip:

pip install -e 'git://github.com/nshah/python-urlencoding.git#egg=urlencoding'

Functions

urlencoding.escape(value)

Escape the string according to:

RFC3986: http://tools.ietf.org/html/rfc3986 http://oauth.net/core/1.0/#encoding_parameters

Arguments:

value
The string to escape.
>>> urlencoding.escape('a b & c')
'a%20b%20%26%20c'
>>> urlencoding.escape('abc123-._~')
'abc123-._~'
urlencoding.parse_qs(query)

Parse a query string into a dict. Values my be strings or arrays.

Arguments:

query
The query string or form encoded body to parse.
>>> urlencoding.parse_qs('a=1&b=%20c+d')
{'a': '1', 'b': ' c d'}
>>> urlencoding.parse_qs('a=2&a=1')
{'a': ['2', '1']}
urlencoding.compose_qs(params, sort=False)

Compose a single string using RFC3986 specified escaping using urlencoding.escape for keys and values.

Arguments:

params
The dict of parameters to encode into a query string.
sort
Boolean indicating if the key/values should be sorted.
>>> urlencoding.compose_qs({'a': '1', 'b': ' c d'})
'a=1&b=%20c%20d'
>>> urlencoding.compose_qs({'a': ['2', '1']})
'a=2&a=1'
>>> urlencoding.compose_qs({'a': ['2', '1', '3']}, sort=True)
'a=1&a=2&a=3'
>>> urlencoding.compose_qs({'a': '1', 'b': {'c': 2, 'd': 3}}, sort=True)
'a=1&b%5Bc%5D=2&b%5Bd%5D=3'