watson.http.headers

class watson.http.headers.HeaderCollection(environ=None)[source]

Retrieves header related variables from an environ.

Allows the use of non-capitalized names.

Example:

headers = HeaderCollection.from_environ(environ)
print(headers.get('Content-Type'))
__init__(environ=None)[source]
add(field, value, replace=False, **options)[source]

Add a header to the collection.

Parameters:
  • field (string) – The field name
  • value (mixed) – The value of the field
  • replace (boolean) – Whether or not to replace an existing field
  • options (kwargs) – Any additional options for the header

Example:

headers = ...
headers.add('Content-Type', 'text/html', charset='utf-8')
classmethod from_environ(environ)[source]

Instantiate the collection from an existing environ.

get(field, option=None, default=None)[source]

Retrieve an individual header or it’s option.

Example:

# Content-Type: text/html; charset=utf-8
headers = HeaderCollection()
headers.add('Content-Type', 'text/html', charset='utf-8')
option = headers.get('Content-Type', 'charset') # utf-8
Parameters:
  • field – the header field
  • option – the option to retrieve from the field
  • default – the default value if the option does not exist
Returns:

The default value or the value from the option

get_option(field, option=None, default=None)

Retrieve an individual header or it’s option.

Example:

# Content-Type: text/html; charset=utf-8
headers = HeaderCollection()
headers.add('Content-Type', 'text/html', charset='utf-8')
option = headers.get('Content-Type', 'charset') # utf-8
Parameters:
  • field – the header field
  • option – the option to retrieve from the field
  • default – the default value if the option does not exist
Returns:

The default value or the value from the option

items()[source]

Returns tuple pairs of environ vars and their values.

set(field, value, **options)[source]

Add a header to the collection.

Any existing headers with the same name will be removed.

Parameters:
  • field (string) – The field name
  • value (mixed) – The value of the field
  • options (kwargs) – Any additional options for the header

Example:

headers = ...
headers.add('Content-Type', 'text/html', charset='utf-8')
class watson.http.headers.ServerCollection(environ=None)[source]

Retrieves server related variables from an environ.

Example:

server = ServerCollection(environ)
print(server['SCRIPT_NAME'])
__init__(environ=None)[source]
items()[source]

Returns tuple pairs of environ vars and their values.

watson.http.headers.convert_to_http_field(field)[source]

Convert a field from Title-Case to HTTP_UPPER_CASE.

watson.http.headers.convert_to_wsgi(field)[source]

Convert a field name from UPPER_CASE to Title-Case.

watson.http.headers.fix_http_headers(environ, remove=False)[source]

Add HTTP_ to the relevant headers that its not included with.