class Response(body=None, status=None, headerlist=None, app_iter=None, content_type=None, conditional_response=None, **kw)[source]

Gets and sets the Accept-Ranges header (HTTP spec section 14.5).


Gets and sets the Age header (HTTP spec section 14.6). Converts it using int.


Gets and sets the Allow header (HTTP spec section 14.7). Converts it using list.


Returns the app_iter of the response.

If body was set, this will create an app_iter from that body (a single-item list)

app_iter_range(start, stop)[source]

Return a new app_iter built from the response app_iter, that serves up only the given start:stop range.


The body of the response, as a str. This will read in the entire app_iter if necessary.


A file-like object that can be used to write to the body. If you passed in a list app_iter, that app_iter will be modified by writes.


Get/set/modify the Cache-Control header (HTTP spec section 14.9)


Get/set the charset (in the Content-Type)

conditional_response_app(environ, start_response)[source]

Like the normal __call__ interface, but checks conditional headers:

  • If-Modified-Since (304 Not Modified; only on GET, HEAD)
  • If-None-Match (304 Not Modified; only on GET, HEAD)
  • Range (406 Partial Content; only on GET, HEAD)

Gets and sets the Content-Disposition header (HTTP spec section 19.5.1).


Gets and sets the Content-Encoding header (HTTP spec section 14.11).


Gets and sets the Content-Language header (HTTP spec section 14.12). Converts it using list.


Gets and sets the Content-Length header (HTTP spec section 14.17). Converts it using int.


Gets and sets the Content-Location header (HTTP spec section 14.14).


Gets and sets the Content-MD5 header (HTTP spec section 14.14).


Gets and sets the Content-Range header (HTTP spec section 14.16). Converts it using ContentRange object.


Get/set the Content-Type header (or None), without the charset or any parameters.

If you include parameters (or ; at all) when setting the content_type, any existing parameters will be deleted; otherwise they will be preserved.


A dictionary of all the parameters in the content type.

(This is not a view, set to change, modifications of the dict would not be applied otherwise)


Makes a copy of the response


Gets and sets the Date header (HTTP spec section 14.18). Converts it using HTTP date.

Delete a cookie from the client. Note that path and domain must match how the cookie was originally set.

This sets the cookie to the empty string, and max_age=0 so that it should expire immediately.

encode_content(encoding='gzip', lazy=False)[source]

Encode the content with the given encoding (only gzip and identity are supported).


Gets and sets the ETag header (HTTP spec section 14.19). Converts it using Entity tag.


Gets and sets the Expires header (HTTP spec section 14.21). Converts it using HTTP date.


Reads a response from a file-like object (it must implement .read(size) and .readline()).

It will read up to the end of the response, not the end of the file.

This reads the response as represented by str(resp); it may not read every valid HTTP response properly. Responses must have a Content-Length


The list of response headers


The headers in a dictionary-like object


Access the body of the response as JSON


Access the body of the response as JSON


Gets and sets the Last-Modified header (HTTP spec section 14.29). Converts it using HTTP date.


Gets and sets the Location header (HTTP spec section 14.30).

md5_etag(body=None, set_content_md5=False)[source]

Generate an etag for the response object using an MD5 hash of the body (the body parameter, or self.body if not given)

Sets self.etag If set_content_md5 is True sets self.content_md5 as well


Merge the cookies that were set on this response with the given resp object (which can be any WSGI application).

If the resp is a webob.Response object, then the other object will be modified in-place.


Gets and sets the Pragma header (HTTP spec section 14.32).


Gets and sets the Retry-After header (HTTP spec section 14.37). Converts it using HTTP date or delta seconds.


Gets and sets the Server header (HTTP spec section 14.38).

Set (add) a cookie for the response.

Arguments are:


The cookie name.


The cookie value, which should be a string or None. If value is None, it’s equivalent to calling the webob.response.Response.unset_cookie() method for this cookie key (it effectively deletes the cookie on the client).


An integer representing a number of seconds or None. If this value is an integer, it is used as the Max-Age of the generated cookie. If expires is not passed and this value is an integer, the max_age value will also influence the Expires value of the cookie (Expires will be set to now + max_age). If this value is None, the cookie will not have a Max-Age value (unless expires is also sent).


A string representing the cookie Path value. It defaults to /.


A string representing the cookie Domain, or None. If domain is None, no Domain value will be sent in the cookie.


A boolean. If it’s True, the secure flag will be sent in the cookie, if it’s False, the secure flag will not be sent in the cookie.


A boolean. If it’s True, the HttpOnly flag will be sent in the cookie, if it’s False, the HttpOnly flag will not be sent in the cookie.


A string representing the cookie Comment value, or None. If comment is None, no Comment value will be sent in the cookie.


A datetime.timedelta object representing an amount of time or the value None. A non-None value is used to generate the Expires value of the generated cookie. If max_age is not passed, but this value is not None, it will influence the Max-Age header (Max-Age will be ‘expires_value - datetime.utcnow()’). If this value is None, the Expires cookie value will be unset (unless max_age is also passed).


If this key is True, before setting the cookie, unset any existing cookie.

The status string


The status as an integer


The status as an integer


Get/set the text value of the body (using the charset of the Content-Type)


Deprecated alias for .text


Deprecated alias for .text

Unset a cookie with the given name (remove it from the response).


Gets and sets the Vary header (HTTP spec section 14.44). Converts it using list.


Gets and sets the WWW-Authenticate header (HTTP spec section 14.47). Converts it using parse_auth and serialize_auth.

class FileResponse(path, request=None, cache_max_age=None, content_type=None, content_encoding=None)[source]

A Response object that can be used to serve a static file from disk simply.

path is a file path on disk.

request must be a Pyramid request object if passed. Note that a request must be passed if the response is meant to attempt to use the wsgi.file_wrapper feature of the web server that you’re using to serve your Pyramid application.

cache_max_age if passed, is the number of seconds that should be used to HTTP cache this response.

content_type, if passed, is the content_type of the response.

content_encoding, if passed is the content_encoding of the response. It’s generally safe to leave this set to None if you’re serving a binary file. This argument will be ignored if you don’t also pass content-type.

class FileIter(file, block_size=262144)[source]

A fixed-block-size iterator for use as a WSGI app_iter.

file is a Python file pointer (or at least an object with a read method that takes a size hint).

block_size is an optional block size for iteration.



Decorator activated via a scan which treats the function being decorated as a response adapter for the set of types or interfaces passed as *types_or_ifaces to the decorator constructor.

For example, if you scan the following response adapter:

from pyramid.response import Response
from pyramid.response import response_adapter

def myadapter(i):
    return Response(status=i)

You can then return an integer from your view callables, and it will be converted into a response with the integer as the status code.

More than one type or interface can be passed as a constructor argument. The decorated response adapter will be called for each type or interface.

import json

from pyramid.response import Response
from pyramid.response import response_adapter

@response_adapter(dict, list)
def myadapter(ob):
    return Response(json.dumps(ob))

This method will have no effect until a scan is performed agains the package or module which contains it, ala:

from pyramid.config import Configurator
config = Configurator()