cyclone.httputil — Manipulate HTTP headers and URLs

HTTP utility code shared by clients and servers.

class cyclone.httputil.HTTPHeaders(*args, **kwargs)[source]

A dictionary that maintains Http-Header-Case for all keys.

Supports multiple values per key via a pair of new methods, add() and get_list(). The regular dictionary interface returns a single value per key, with multiple values joined by a comma.

>>> h = HTTPHeaders({"content-type": "text/html"})
>>> list(h.keys())
>>> h["Content-Type"]
>>> h.add("Set-Cookie", "A=B")
>>> h.add("Set-Cookie", "C=D")
>>> h["set-cookie"]
>>> h.get_list("set-cookie")
['A=B', 'C=D']
>>> for (k,v) in sorted(h.get_all()):
...    print('%s: %s' % (k,v))
Content-Type: text/html
Set-Cookie: A=B
Set-Cookie: C=D
add(name, value)[source]

Adds a new value for the given key.


Returns all values for the given header as a list.


Returns an iterable of all (name, value) pairs.

If a header has multiple values, multiple pairs will be returned with the same name.


Updates the dictionary with a single header line.

>>> h = HTTPHeaders()
>>> h.parse_line("Content-Type: text/html")
>>> h.get('content-type')
classmethod parse(headers)[source]

Returns a dictionary from HTTP header text.

>>> h = HTTPHeaders.parse(
    "Content-Type: text/html\r\nContent-Length: 42\r\n")
>>> sorted(h.items())
[('Content-Length', '42'), ('Content-Type', 'text/html')]
cyclone.httputil.url_concat(url, args)[source]

Concatenate url and argument dictionary regardless of whether url has existing query parameters.

>>> url_concat("", dict(c="d"))
class cyclone.httputil.HTTPFile[source]

Represents an HTTP file. For backwards compatibility, its instance attributes are also accessible as dictionary keys.

  • filename
  • body
  • content_type – The content_type comes from the provided HTTP header and should not be trusted outright given that it can be easily forged.
cyclone.httputil.parse_body_arguments(content_type, body, arguments, files)[source]

Parses a form request body.

Supports “application/x-www-form-urlencoded” and “multipart/form-data”. The content_type parameter should be a string and body should be a byte string. The arguments and files parameters are dictionaries that will be updated with the parsed contents.

cyclone.httputil.parse_multipart_form_data(boundary, data, arguments, files)[source]

Parses a multipart/form-data body.

The boundary and data parameters are both byte strings. The dictionaries given in the arguments and files parameters will be updated with the contents of the body.

Previous topic

cyclone.httpserver — Non-blocking HTTP server

Next topic

cyclone.template — Flexible output generation

This Page