classResponse(ResponseBase, JSONMixin):"""The response object that is used by default in Flask. Works like the
response object from Werkzeug but is set to have an HTML mimetype by
default. Quite often you don't have to create this object yourself because
:meth:`~flask.Flask.make_response` will take care of that for you.
在Flask中默认使用的响应对象。类似于来自Werkzeug的响应对象,
但默认情况下设置为具有HTML mimetype。通常,您不必自己创建该对象,
因为:meth:`〜 flask.Flask.make_response`会帮您解决这个问题。
If you want to replace the response object used you can subclass this and
set :attr:`~flask.Flask.response_class` to your subclass.
如果要替换使用的响应对象,则可以将其子类化,然后将:attr:`〜flask.Flask.response_class`设置为子类。
.. versionchanged:: 1.0
JSON support is added to the response, like the request. This is useful
when testing to get the test client response data as JSON.
JSON支持已添加到响应(如请求)中。
在进行测试以将测试客户端响应数据作为JSON获取时,这很有用。
.. versionchanged:: 1.0
Added :attr:`max_cookie_size`.
"""
default_mimetype ="text/html"def_get_data_for_json(self, cache):return self.get_data()
@propertydefmax_cookie_size(self):"""Read-only view of the :data:`MAX_COOKIE_SIZE` config key.
See :attr:`~werkzeug.wrappers.BaseResponse.max_cookie_size` in
Werkzeug's docs.
"""if current_app:return current_app.config["MAX_COOKIE_SIZE"]# return Werkzeug's default when not in an app contextreturnsuper(Response, self).max_cookie_size
classResponse(
BaseResponse,
ETagResponseMixin,
WWWAuthenticateMixin,
CORSResponseMixin,
ResponseStreamMixin,
CommonResponseDescriptorsMixin,):"""Full featured response object implementing the following mixins:
- :class:`ETagResponseMixin` for etag and cache control handling
- :class:`WWWAuthenticateMixin` for HTTP authentication support
- :class:`~werkzeug.wrappers.cors.CORSResponseMixin` for Cross
Origin Resource Sharing headers
- :class:`ResponseStreamMixin` to add support for the ``stream``
property
- :class:`CommonResponseDescriptorsMixin` for various HTTP
descriptors
"""
功能齐全的响应对象,实现以下mixin:
-:ETagResponseMixin类,用于etag和缓存控件处理
-:WWWAuthenticateMixin`类,用于HTTP身份验证支持
-:class:`〜werkzeug.wrappers.cors.CORSResponseMixin` for Cross
源资源共享标头
-:ResponseStreamMixin`增加对流的支持
属性
-各种HTTP的CommonResponseDescriptorsMixin`
描述符
classBaseResponse(object):"""Base response class. The most important fact about a response object
is that it's a regular WSGI application. It's initialized with a couple
of response parameters (headers, body, status code etc.) and will start a
valid WSGI response when called with the environ and start response
callable.
Because it's a WSGI application itself processing usually ends before the
actual response is sent to the server. This helps debugging systems
because they can catch all the exceptions before responses are started.
基本响应类。 关于响应对象的最重要事实是它是常规的WSGI应用程序。
它使用几个响应参数(标头,正文,状态码等)进行了初始化,
并且在使用环境调用时将启动有效的WSGI响应,并启动可调用的响应。
因为它是WSGI应用程序,所以处理通常会在实际响应发送到服务器之前结束。
这有助于调试系统,因为它们可以在响应开始之前捕获所有异常。
Here a small example WSGI application that takes advantage of the
response objects::
这里有一个小示例WSGI应用程序,它利用了
响应对象:
from werkzeug.wrappers import BaseResponse as Response
def index():
return Response('Index page')
def application(environ, start_response):
path = environ.get('PATH_INFO') or '/'
if path == '/':
response = index()
else:
response = Response('Not Found', status=404)
return response(environ, start_response)
Like :class:`BaseRequest` which object is lacking a lot of functionality
implemented in mixins. This gives you a better control about the actual
API of your response objects, so you can create subclasses and add custom
functionality. A full featured response object is available as
:class:`Response` which implements a couple of useful mixins.
像BaseRequest这样的class,该对象缺少很多在mixin中实现的功能。
这样可以更好地控制响应对象的实际API,因此您可以创建子类并添加自定义功能。
一个功能齐全的响应对象可以作为:class:`Response`来实现,它实现了几个有用的mixin。
To enforce a new type of already existing responses you can use the
:meth:`force_type` method. This is useful if you're working with different
subclasses of response objects and you want to post process them with a
known interface.
要强制使用一种新的已经存在的响应,可以使用:meth:`force_type`方法。
如果您正在使用响应对象的不同子类,并且希望使用已知接口对它们进行后期处理,这将非常有用。
Per default the response object will assume all the text data is `utf-8`
encoded. Please refer to :doc:`the unicode chapter </unicode>` for more
details about customizing the behavior.
Response can be any kind of iterable or string. If it's a string it's
considered being an iterable with one item which is the string passed.
Headers can be a list of tuples or a
:class:`~werkzeug.datastructures.Headers` object.
Special note for `mimetype` and `content_type`: For most mime types
`mimetype` and `content_type` work the same, the difference affects
only 'text' mimetypes. If the mimetype passed with `mimetype` is a
mimetype starting with `text/`, the charset parameter of the response
object is appended to it. In contrast the `content_type` parameter is
always added as header unmodified.
默认情况下,响应对象将假定所有文本数据都是“ utf-8”编码的。
有关自定义行为的更多详细信息,请参考unicode章节</ unicode>。
响应可以是任何类型的可迭代或字符串。 如果是字符串,则认为它是可迭代的,
且其中一项是传递的字符串。
标头可以是元组的列表,也可以是〜class:`〜werkzeug.datastructures.Headers`对象。
对于“ mimetype”和“ content_type”的特殊说明:对于大多数mime类型,
“ mimetype”和“ content_type”的工作原理相同,区别仅影响“文本” mimetype。
如果以`mimetype`传递的mimetype是以`text /`开头的mimetype,
则将响应对象的charset参数附加到其上。
相反,“ content_type”参数总是作为未修改的标头添加。
.. versionchanged:: 0.5
the `direct_passthrough` parameter was added.
:param response: a string or response iterable.
:param status: a string with a status or an integer with the status code.
:param headers: a list of headers or a
:class:`~werkzeug.datastructures.Headers` object.
:param mimetype: the mimetype(模仿型) for the response. See notice above.
:param content_type: the content type for the response. See notice above.
:param direct_passthrough: if set to `True` :meth:`iter_encoded` is not
called before iteration which makes it
possible to pass special iterators through
unchanged (see :func:`wrap_file` for more
details.)
"""