webob.response -- Response¶
Response¶
-
class
webob.response.Response(body=None, status=None, headerlist=None, app_iter=None, content_type=None, conditional_response=None, charset=<object object>, **kw)¶ Represents a WSGI response.
If no arguments are passed, creates a
Responsethat uses a variety of defaults. The defaults may be changed by sub-classing theResponse. See the sub-classing notes.- Variables
body (bytes or text_type) -- If
bodyis atext_type, then it will be encoded using eithercharsetwhen provided ordefault_encodingwhencharsetis not provided if thecontent_typeallows for acharset. This argument is mutually exclusive withapp_iter.status (int or str) -- Either an
intor a string that is an integer followed by the status text. If it is an integer, it will be converted to a proper status that also includes the status text. Any existing status text will be kept. Non-standard values are allowed.headerlist (list) -- A list of HTTP headers for the response.
app_iter (iterable) -- An iterator that is used as the body of the response. Should conform to the WSGI requirements and should provide bytes. This argument is mutually exclusive with
body.content_type (str or None) -- Sets the
Content-Typeheader. If nocontent_typeis provided, and there is noheaderlist, thedefault_content_typewill be automatically set. Ifheaderlistis provided then this value is ignored.conditional_response (bool) -- Used to change the behavior of the
Responseto check the original request for conditional response headers. Seeconditional_response_app()for more information.charset (str or None) -- Adds a
charsetContent-Typeparameter. If nocharsetis provided and theContent-Typeis text, then thedefault_charsetwill automatically be added. Currently the onlyContent-Type's that allow for acharsetare defined to betext/*,application/xml, and*/*+xml. Any otherContent-Type's will not have acharsetadded. If aheaderlistis provided this value is ignored.
All other response attributes may be set on the response by providing them as keyword arguments. A
TypeErrorwill be raised for any unexpected keywords.Sub-classing notes:
The
default_content_typeis used as the default for theContent-Typeheader that is returned on the response. It istext/html.The
default_charsetis used as the default character set to return on theContent-Typeheader, if theContent-Typeallows for acharsetparameter. Currently the onlyContent-Type's that allow for acharsetare defined to be:text/*,application/xml, and*/*+xml. Any otherContent-Type's will not have acharsetadded.The
unicode_errorsis set tostrict, and access on atextwill raise an error if it fails to decode thebody.default_conditional_responseis set toFalse. This flag may be set toTrueso that allResponseobjects will attempt to check the original request for conditional response headers. Seeconditional_response_app()for more information.default_body_encodingis set to 'UTF-8' by default. It exists to allow users to get/set theResponseobject using.text, even if nocharsethas been set for theContent-Type.
-
classmethod
from_file(fp)¶ 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.
-
copy()¶ Makes a copy of the response.
-
property
status¶ The status string.
-
property
status_code¶ The status as an integer.
-
property
status_int¶ The status as an integer.
-
property
headerlist¶ The list of response headers.
-
property
headers¶ The headers in a dictionary-like object.
-
property
body¶ The body of the response, as a
bytes. This will read in the entire app_iter if necessary.
-
property
json¶ Set/get the body of the response as JSON.
-
property
json_body¶ Set/get the body of the response as JSON.
-
property
has_body¶ Determine if the the response has a
body. In contrast to simply accessingbody, this method will not read the underlyingapp_iter.
-
property
text¶ Get/set the text value of the body using the
charsetof theContent-Typeor thedefault_body_encoding.
-
property
unicode_body¶ Deprecated alias for .text
-
property
ubody¶ Deprecated alias for .text
-
property
body_file¶ A file-like object that can be used to write to the body. If you passed in a list
app_iter, thatapp_iterwill be modified by writes.
-
property
app_iter¶ Returns the
app_iterof the response.If
bodywas set, this will create anapp_iterfrom thatbody(a single-item list).
-
property
allow¶ Gets and sets the
Allowheader (HTTP spec section 14.7). Converts it using list.
-
property
vary¶ Gets and sets the
Varyheader (HTTP spec section 14.44). Converts it using list.
-
property
content_length¶ Gets and sets the
Content-Lengthheader (HTTP spec section 14.17). Converts it using int.
-
property
content_encoding¶ Gets and sets the
Content-Encodingheader (HTTP spec section 14.11).
-
property
content_language¶ Gets and sets the
Content-Languageheader (HTTP spec section 14.12). Converts it using list.
-
property
content_location¶ Gets and sets the
Content-Locationheader (HTTP spec section 14.14).
-
property
content_md5¶ Gets and sets the
Content-MD5header (HTTP spec section 14.14).
-
property
content_disposition¶ Gets and sets the
Content-Dispositionheader (HTTP spec section 19.5.1).
-
property
accept_ranges¶ Gets and sets the
Accept-Rangesheader (HTTP spec section 14.5).
-
property
content_range¶ Gets and sets the
Content-Rangeheader (HTTP spec section 14.16). Converts it using ContentRange object.
-
property
date¶ Gets and sets the
Dateheader (HTTP spec section 14.18). Converts it using HTTP date.
-
property
expires¶ Gets and sets the
Expiresheader (HTTP spec section 14.21). Converts it using HTTP date.
-
property
last_modified¶ Gets and sets the
Last-Modifiedheader (HTTP spec section 14.29). Converts it using HTTP date.
-
property
etag¶ Gets and sets the
ETagheader (HTTP spec section 14.19). Converts it using Entity tag.
-
property
location¶ Gets and sets the
Locationheader (HTTP spec section 14.30).
-
property
pragma¶ Gets and sets the
Pragmaheader (HTTP spec section 14.32).
-
property
age¶ Gets and sets the
Ageheader (HTTP spec section 14.6). Converts it using int.
-
property
retry_after¶ Gets and sets the
Retry-Afterheader (HTTP spec section 14.37). Converts it using HTTP date or delta seconds.
-
property
server¶ Gets and sets the
Serverheader (HTTP spec section 14.38).
-
property
www_authenticate¶ Gets and sets the
WWW-Authenticateheader (HTTP spec section 14.47). Converts it usingparse_authandserialize_auth.
-
property
charset¶ Get/set the
charsetspecified inContent-Type.There is no checking to validate that a
content_typeactually allows for acharsetparameter.
-
property
content_type¶ Get/set the
Content-Typeheader. If noContent-Typeheader is set, this will returnNone.Changed in version 1.7: Setting a new
Content-Typewill remove allContent-Typeparameters and reset thecharsetto the default if theContent-Typeistext/*or XML (application/xmlor*/*+xml).To preserve all
Content-Typeparameters, you may use the following code:resp = Response() params = resp.content_type_params resp.content_type = 'application/something' resp.content_type_params = params
-
property
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 will not be applied otherwise.)
Set (add) a cookie for the response.
Arguments are:
nameThe cookie name.
valueThe cookie value, which should be a string or
None. 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,
datetime.timedelta, orNone. This value is used as theMax-Ageof the generated cookie. Ifexpiresis not passed and this value is notNone, themax_agevalue will also influence theExpiresvalue of the cookie (Expireswill be set tonow+max_age). If this value isNone, the cookie will not have aMax-Agevalue (unlessexpiresis set). If bothmax_ageandexpiresare set, this value takes precedence.pathA string representing the cookie
Pathvalue. It defaults to/.domainA string representing the cookie
Domain, orNone. If domain isNone, noDomainvalue will be sent in the cookie.secureA boolean. If it's
True, thesecureflag will be sent in the cookie, if it'sFalse, thesecureflag will not be sent in the cookie.httponlyA boolean. If it's
True, theHttpOnlyflag will be sent in the cookie, if it'sFalse, theHttpOnlyflag will not be sent in the cookie.samesiteA string representing the
SameSiteattribute of the cookie orNone. If samesite isNonenoSameSitevalue will be sent in the cookie. Should only be"Strict"or"Lax".commentA string representing the cookie
Commentvalue, orNone. IfcommentisNone, noCommentvalue will be sent in the cookie.expiresA
datetime.timedeltaobject representing an amount of time,datetime.datetimeorNone. 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. If this value isNone, theExpirescookie value will be unset (unlessmax_ageis set). Ifmax_ageis set, it will be used to generate theexpiresand this value is ignored.If a
datetime.datetimeis provided it has to either be timezone aware or be based on UTC.datetime.datetimeobjects that are local time are not supported. Timezone awaredatetime.datetimeobjects are converted to UTC.This argument will be removed in future versions of WebOb (version 1.9).
overwriteIf this key is
True, before setting the cookie, unset any existing cookie.
Delete a cookie from the client. Note that
pathanddomainmust match how the cookie was originally set.This sets the cookie to the empty string, and
max_age=0so that it should expire immediately.
Unset a cookie with the given name (remove it from the response).
Merge the cookies that were set on this response with the given
respobject (which can be any WSGI application).If the
respis awebob.Responseobject, then the other object will be modified in-place.
-
property
cache_control¶ Get/set/modify the Cache-Control header (HTTP spec section 14.9).
-
encode_content(encoding='gzip', lazy=False)¶ Encode the content with the given encoding (only
gzipandidentityare supported).
-
md5_etag(body=None, set_content_md5=False)¶ Generate an etag for the response object using an MD5 hash of the body (the
bodyparameter, orself.bodyif not given).Sets
self.etag.If
set_content_md5isTrue, setsself.content_md5as well.
-
conditional_response_app(environ, start_response)¶ Like the normal
__call__interface, but checks conditional headers:If-Modified-Since(304 Not Modified; only onGET,HEAD)If-None-Match(304 Not Modified; only onGET,HEAD)Range(406 Partial Content; only onGET,HEAD)
-
app_iter_range(start, stop)¶ Return a new
app_iterbuilt from the responseapp_iter, that serves up only the givenstart:stoprange.
-
class
webob.response.ResponseBodyFile(response)¶ -
property
encoding¶ The encoding of the file (inherited from response.charset)
-
writelines(seq)¶ Write a sequence of lines to the response.
-
tell()¶ Provide the current location where we are going to start writing.
-
property
-
class
webob.response.AppIterRange(app_iter, start, stop)¶ Wraps an
app_iter, returning just a range of bytes.