pyramid.response¶
-
class
Response(body=None, status=None, headerlist=None, app_iter=None, content_type=None, conditional_response=None, **kw)[source]¶ -
accept_ranges¶ Gets and sets the
Accept-Rangesheader (HTTP spec section 14.5).
-
age¶ Gets and sets the
Ageheader (HTTP spec section 14.6). Converts it using int.
-
allow¶ Gets and sets the
Allowheader (HTTP spec section 14.7). Converts it using list.
-
app_iter¶ 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:stoprange.
-
body¶ The body of the response, as a
str. This will read in the entire app_iter if necessary.
-
body_file¶ 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.
-
cache_control¶ Get/set/modify the Cache-Control header (HTTP spec section 14.9)
-
charset¶ 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)
-
content_disposition¶ Gets and sets the
Content-Dispositionheader (HTTP spec section 19.5.1).
-
content_encoding¶ Gets and sets the
Content-Encodingheader (HTTP spec section 14.11).
-
content_language¶ Gets and sets the
Content-Languageheader (HTTP spec section 14.12). Converts it using list.
-
content_length¶ Gets and sets the
Content-Lengthheader (HTTP spec section 14.17). Converts it using int.
-
content_location¶ Gets and sets the
Content-Locationheader (HTTP spec section 14.14).
-
content_md5¶ Gets and sets the
Content-MD5header (HTTP spec section 14.14).
-
content_range¶ Gets and sets the
Content-Rangeheader (HTTP spec section 14.16). Converts it using ContentRange object.
-
content_type¶ 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.
-
content_type_params¶ 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)
-
date¶ Gets and sets the
Dateheader (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).
-
etag¶ Gets and sets the
ETagheader (HTTP spec section 14.19). Converts it using Entity tag.
-
expires¶ Gets and sets the
Expiresheader (HTTP spec section 14.21). Converts it using HTTP date.
-
from_file(fp)[source]¶ 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 aContent-Length
-
headerlist¶ The list of response headers
-
headers¶ The headers in a dictionary-like object
-
json¶ Access the body of the response as JSON
-
json_body¶ Access the body of the response as JSON
-
last_modified¶ Gets and sets the
Last-Modifiedheader (HTTP spec section 14.29). Converts it using HTTP date.
-
location¶ Gets and sets the
Locationheader (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.bodyif not given)Sets
self.etagIfset_content_md5is True setsself.content_md5as 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.Responseobject, then the other object will be modified in-place.
-
pragma¶ Gets and sets the
Pragmaheader (HTTP spec section 14.32).
-
retry_after¶ Gets and sets the
Retry-Afterheader (HTTP spec section 14.37). Converts it using HTTP date or delta seconds.
-
server¶ Gets and sets the
Serverheader (HTTP spec section 14.38).
Set (add) a cookie for the response.
Arguments are:
keyThe cookie name.valueThe cookie value, which should be a string orNone. IfvalueisNone, it’s equivalent to calling thewebob.response.Response.unset_cookie()method for this cookie key (it effectively deletes the cookie on the client).max_ageAn integer representing a number of seconds orNone. If this value is an integer, it is used as theMax-Ageof the generated cookie. Ifexpiresis not passed and this value is an integer, themax_agevalue will also influence theExpiresvalue of the cookie (Expireswill be set to now + max_age). If this value isNone, the cookie will not have aMax-Agevalue (unlessexpiresis also sent).pathA string representing the cookiePathvalue. It defaults to/.domainA string representing the cookieDomain, orNone. If domain isNone, noDomainvalue will be sent in the cookie.secureA boolean. If it’sTrue, thesecureflag will be sent in the cookie, if it’sFalse, thesecureflag will not be sent in the cookie.httponlyA boolean. If it’sTrue, theHttpOnlyflag will be sent in the cookie, if it’sFalse, theHttpOnlyflag will not be sent in the cookie.commentA string representing the cookieCommentvalue, orNone. IfcommentisNone, noCommentvalue will be sent in the cookie.expiresAdatetime.timedeltaobject representing an amount of time or the valueNone. A non-Nonevalue is used to generate theExpiresvalue of the generated cookie. Ifmax_ageis not passed, but this value is notNone, it will influence theMax-Ageheader (Max-Agewill be ‘expires_value - datetime.utcnow()’). If this value isNone, theExpirescookie value will be unset (unlessmax_ageis also passed).overwriteIf this key isTrue, before setting the cookie, unset any existing cookie.
-
status¶ The status string
-
status_code¶ The status as an integer
-
status_int¶ The status as an integer
-
text¶ Get/set the text value of the body (using the charset of the Content-Type)
-
ubody¶ Deprecated alias for .text
-
unicode_body¶ Deprecated alias for .text
Unset a cookie with the given name (remove it from the response).
-
vary¶ Gets and sets the
Varyheader (HTTP spec section 14.44). Converts it using list.
-
www_authenticate¶ Gets and sets the
WWW-Authenticateheader (HTTP spec section 14.47). Converts it usingparse_authandserialize_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.
pathis a file path on disk.requestmust be a Pyramid request object if passed. Note that a request must be passed if the response is meant to attempt to use thewsgi.file_wrapperfeature of the web server that you’re using to serve your Pyramid application.cache_max_ageif 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 toNoneif you’re serving a binary file. This argument will be ignored if you don’t also passcontent-type.
-
class
FileIter(file, block_size=262144)[source]¶ A fixed-block-size iterator for use as a WSGI app_iter.
fileis a Python file pointer (or at least an object with areadmethod that takes a size hint).block_sizeis an optional block size for iteration.
Functions¶
-
response_adapter(*types_or_ifaces)[source]¶ 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_ifacesto the decorator constructor.For example, if you scan the following response adapter:
from pyramid.response import Response from pyramid.response import response_adapter @response_adapter(int) 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() config.scan('somepackage_containing_adapters')