util.url

Module Contents

Classes

Url() Datastructure for representing an HTTP URL. Used as a return value for

Functions

split_first(s,delims) Given a string and an iterable of delimiters, split on the first found
parse_url(url) Given a url, return a parsed Url namedtuple. Best-effort is
get_host(url) Deprecated. Use parse_url() instead.
class Url

Datastructure for representing an HTTP URL. Used as a return value for parse_url().

__new__(scheme=None, auth=None, host=None, port=None, path=None, query=None, fragment=None)
hostname()

For backwards-compatibility with urlparse. We’re nice like that.

request_uri()

Absolute path including the query string.

netloc()

Network location including host and port

url()

Convert self into a url

This function should more or less round-trip with parse_url(). The returned url may not be exactly the same as the url inputted to parse_url(), but it should be equivalent by the RFC (e.g., urls with a blank port will have : removed).

Example:

>>> U = parse_url('http://google.com/mail/')
>>> U.url
'http://google.com/mail/'
>>> Url('http', 'username:password', 'host.com', 80,
... '/path', 'query', 'fragment').url
'http://username:password@host.com:80/path?query#fragment'
__str__()
split_first(s, delims)

Given a string and an iterable of delimiters, split on the first found delimiter. Return two split parts and the matched delimiter.

If not found, then the first part is the full input string.

Example:

>>> split_first('foo/bar?baz', '?/=')
('foo', 'bar?baz', '/')
>>> split_first('foo/bar?baz', '123')
('foo/bar?baz', '', None)

Scales linearly with number of delims. Not ideal for large number of delims.

parse_url(url)

Given a url, return a parsed Url namedtuple. Best-effort is performed to parse incomplete urls. Fields not provided will be None.

Partly backwards-compatible with urlparse.

Example:

>>> parse_url('http://google.com/mail/')
Url(scheme='http', host='google.com', port=None, path='/mail/', ...)
>>> parse_url('google.com:80')
Url(scheme=None, host='google.com', port=80, path=None, ...)
>>> parse_url('/foo?bar')
Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...)
get_host(url)

Deprecated. Use parse_url() instead.