circuits.web.headers – Headers

Headers Support

This module implements support for parsing and handling headers.

Events

none

Classes

class circuits.web.headers.Headers(headers=[])

Bases: dict

Manage a collection of HTTP response headers

has_key(name)

Return true if the message contains the header.

get_all(name)

Return a list of all the values for the named field.

These will be sorted in the order they appeared in the original header list or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no fields exist with the given name, returns an empty list.

get(name, default=None)

Get the first header value for ‘name’, or return ‘default’

keys()

Return a list of all the header field names.

These will be sorted in the order they appeared in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

values()

Return a list of all header values.

These will be sorted in the order they appeared in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

items()

Get all the header fields and values.

These will be sorted in the order they were in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

setdefault(name, value)

Return first matching header value for ‘name’, or ‘value’

If there is no header named ‘name’, add a new header with name ‘name’ and value ‘value’.

add_header(_name, _value, **_params)

Extended header setting.

_name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key=”value” unless value is None, in which case only the key will be added.

Example:

h.add_header(‘content-disposition’, ‘attachment’, filename=’bud.gif’)

Note that unlike the corresponding ‘email.Message’ method, this does not handle ‘(charset, language, value)’ tuples: all values must be strings or None.

elements(key)

Return a list of HeaderElements for the given header (or None).

class circuits.web.headers.HeaderElement(value, params=None)

Bases: object

An element (with parameters) from an HTTP header’s element list.

static parse(elementstr)

Transform ‘token;key=val’ to (‘token’, {‘key’: ‘val’}).

classmethod from_str(elementstr)

Construct an instance from a string of the form ‘token;key=val’.

class circuits.web.headers.AcceptElement(value, params=None)

Bases: circuits.web.headers.HeaderElement

An element (with parameters) from an Accept* header’s element list.

AcceptElement objects are comparable; the more-preferred object will be “less than” the less-preferred object. They are also therefore sortable; if you sort a list of AcceptElement objects, they will be listed in priority order; the most preferred value will be first. Yes, it should have been the other way around, but it’s too late to fix now.

qvalue

The qvalue, or priority, of this value.

Components

none

Functions

circuits.web.headers.header_elements(fieldname, fieldvalue)

Return a HeaderElement list from a comma-separated header str.

circuits.web.headers.parse_headers(data)