-
Notifications
You must be signed in to change notification settings - Fork 1
/
urls.py
32 lines (24 loc) · 1.02 KB
/
urls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from urllib.parse import ParseResult, urlencode, urlparse, urlunparse
def url(scheme='', netloc='', path='', params='', query=(), fragment=''):
"""Construct a URL from individual components.
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
Args:
schema: "http", "https", etc
netloc: hostname (or <username>:<password>@<hostname>:<port>)
path: "/path/to/resource". Leading slash is automatic.
params: Rarely used; you probably want query.
query: May be a dict or a sequence of 2-tuples.
fragment: Position on page; usually used only by the browser.
"""
return urlunparse(
(scheme, netloc, path, params, urlencode(query), fragment))
def alter_url(url, **components):
"""
>>> alter_url('http://placekitten.com', path='200/300')
'http://placekitten.com/200/300'
"""
original = urlparse(url)._asdict()
original.update(components)
return ParseResult(**original).geturl()
def query_string(**kwargs):
return '?' + urlencode(kwargs)