Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

18.4.2wsgiref.headers - WSGI response header tools

This module provides a single class,Headers, for convenientmanipulation of WSGI response headers using a mapping-like interface.

class Headers(headers)
Create a mapping-like object wrappingheaders, which must be alist of header name/value tuples as described inPEP 333. Any changesmade to the newHeaders object will directly update theheaders list it was created with.

Headers objects support typical mapping operations including__getitem__,get,__setitem__,setdefault,__delitem__,__contains__ andhas_key. For each of these methods, the key is the header name(treated case-insensitively), and the value is the first valueassociated with that header name. Setting a header deletes any existingvalues for that header, then adds a new value at the end of the wrappedheader list. Headers' existing order is generally maintained, with newheaders added to the end of the wrapped list.

Unlike a dictionary,Headers objects do not raise an error whenyou try to get or delete a key that isn't in the wrapped header list.Getting a nonexistent header just returnsNone, and deletinga nonexistent header does nothing.

Headers objects also supportkeys(),values(),anditems() methods. The lists returned bykeys()anditems() can include the same key more than once if thereis a multi-valued header. Thelen() of aHeaders objectis the same as the length of itsitems(), which is the sameas the length of the wrapped header list. In fact, theitems()method just returns a copy of the wrapped header list.

Callingstr() on aHeaders object returns a formattedstring suitable for transmission as HTTP response headers. Each headeris placed on a line with its value, separated by a colon and a space.Each line is terminated by a carriage return and line feed, and thestring is terminated with a blank line.

In addition to their mapping interface and formatting features,Headers objects also have the following methods for queryingand adding multi-valued headers, and for adding headers with MIMEparameters:

get_all(name)
Return a list of all the values for the named header.

The returned list will be sorted in the order they appeared in theoriginal header list or were added to this instance, and may containduplicates. Any fields deleted and re-inserted are always appended tothe header list. If no fields exist with the given name, returns anempty list.

add_header(name, value, **_params)
Add a (possibly multi-valued) header, with optional MIME parametersspecified via keyword arguments.

name is the header field to add. Keyword arguments can be used toset MIME parameters for the header field. Each parameter must be astring orNone. Underscores in parameter names are converted todashes, since dashes are illegal in Python identifiers, but many MIMEparameter names include dashes. If the parameter value is a string, itis added to the header value parameters in the formname="value".If it isNone, only the parameter name is added. (This is usedfor MIME parameters without a value.) Example usage:

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

The above will add a header that looks like this:

Content-Disposition: attachment; filename="bud.gif"


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp