flask 源码解析:响应

2024-09-02 12:58
文章标签 源码 解析 响应 flask

本文主要是介绍flask 源码解析:响应,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载于:http://cizixs.com/2017/01/22/flask-insight-response

response 简介

在 flask 应用中,我们只需要编写 view 函数,并不需要直接和响应(response)打交道,flask 会自动生成响应返回给客户端。

The return value from a view function is automatically converted into a response object for you. —— Flask docs

我们知道 HTTP 响应分为三个部分: 状态栏(HTTP 版本、状态码和说明)、头部(以冒号隔开的字符对,用于各种控制和协商)、body(服务端返回的数据)。比如下面访问博客首页的响应:

HTTP/1.1 200 OKAccess-Control-Allow-Origin: *
Cache-Control: max-age=600
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Wed, 15 Feb 2017 07:50:41 GMT
Expires: Wed, 15 Feb 2017 08:00:41 GMT
Last-Modified: Wed, 15 Feb 2017 07:46:56 GMT
Server: GitHub.com
Transfer-Encoding: chunked
X-GitHub-Request-Id: D2A7:7B6B:33C0628:47C44B9:58A40851<BODY>

flask 自然也会提供所有这些数据的操作,视图函数就支持返回三个值:第一个是返回的数据,第二个是状态码,第三个是头部字典。比如:

@app.route('/')
def hello_world():return 'Hello, World!', 201, {'X-Foo': 'bar'}

这篇文章就讲讲这背后的魔法。

flask 响应(response)

在 flask 源码解析:应用启动流程 的最后,我们讲到 full_dispatch_request 在调用路由的视图函数之后,会调用 finalize_request 进行最后的处理,在这个方法里就包含了 response 对象的生成和处理逻辑。

finalize_request 的代码如下:

def finalize_request(self, rv, from_error_handler=False):"""Given the return value from a view function this finalizesthe request by converting it into a response and invoking thepostprocessing functions.  This is invoked for both normalrequest dispatching as well as error handlers."""response = self.make_response(rv)try:response = self.process_response(response)request_finished.send(self, response=response)except Exception:if not from_error_handler:raiseself.logger.exception('Request finalizing failed with an ''error while handling an error')return response

里面有两个方法调用:make_response 根据视图函数的返回值生成 response 对象,process_response 对 response 做一些后续的处理(比如执行 hooks 函数)。我们先来看看 make_response

def make_response(self, rv):"""Converts the return value from a view function to a realresponse object that is an instance of :attr:`response_class`."""status_or_headers = headers = Noneif isinstance(rv, tuple):rv, status_or_headers, headers = rv + (None,) * (3 - len(rv))if isinstance(status_or_headers, (dict, list)):headers, status_or_headers = status_or_headers, Noneif not isinstance(rv, self.response_class):# When we create a response object directly, we let the constructor# set the headers and status.  We do this because there can be# some extra logic involved when creating these objects with# specific values (like default content type selection).if isinstance(rv, (text_type, bytes, bytearray)):rv = self.response_class(rv, headers=headers,status=status_or_headers)headers = status_or_headers = Noneif status_or_headers is not None:if isinstance(status_or_headers, string_types):rv.status = status_or_headerselse:rv.status_code = status_or_headersif headers:rv.headers.extend(headers)return rv

make_response 是视图函数能返回多个不同数量和类型值的关键,因为它能处理这些情况,统一把它们转换成 response。 如果返回值本身就是 Response 实例,就直接使用它;如果返回值是字符串类型,就把它作为响应的 body,并自动设置状态码和头部信息; 如果返回值是 tuple,会尝试用 (response, status, headers) 或者 (response, headers) 去解析。

NOTE:因为视图函数可以返回 Response 对象,因此我们可以直接操作 Response

不管视图函数返回的是什么,最终都会变成 Response 对象,那么我们就来看看 Response的定义:

from werkzeug.wrappers import Response as ResponseBaseclass Response(ResponseBase):"""The response object that is used by default in Flask.  Works like theresponse object from Werkzeug but is set to have an HTML mimetype bydefault.  Quite often you don't have to create this object yourself because:meth:`~flask.Flask.make_response` will take care of that for you.If you want to replace the response object used you can subclass this andset :attr:`~flask.Flask.response_class` to your subclass."""default_mimetype = 'text/html'

Flask 的 Response 类非常简单,它只是继承了 werkzeug.wrappers:Response,然后设置默认返回类型为 html。 不过从注释中,我们得到两条很有用的信息:

  1. 一般情况下不要直接操作 Response 对象,而是使用 make_response 方法来生成它
  2. 如果需要使用自定义的响应对象,可以覆盖 flask app 对象的 response_class 属性。

继续,下面就要分析 werkzeug 对应的代码了。

werkzeug response

werkzeug 实现的 response 定义在 werkzeug/wrappers.py 文件中:

class Response(BaseResponse, ETagResponseMixin, ResponseStreamMixin,CommonResponseDescriptorsMixin,WWWAuthenticateMixin):"""Full featured response object implementing the following mixins:- :class:`ETagResponseMixin` for etag and cache control handling- :class:`ResponseStreamMixin` to add support for the `stream` property- :class:`CommonResponseDescriptorsMixin` for various HTTP descriptors- :class:`WWWAuthenticateMixin` for HTTP authentication support"""

和我们在 flask 请求分析的 Request 类一样,这里使用了 Mixin 机制。BaseResponse 精简后的大概框架如下:

class BaseResponse(object):"""Base response class.  The most important fact about a response objectis that it's a regular WSGI application.  It's initialized with a coupleof response parameters (headers, body, status code etc.) and will start avalid WSGI response when called with the environ and start responsecallable."""charset = 'utf-8'default_status = 200default_mimetype = 'text/plain'automatically_set_content_length = Truedef __init__(self, response=None, status=None, headers=None,mimetype=None, content_type=None, direct_passthrough=False):pass

BaseResponse 有一些类属性,定义了默认的值,比如默认字符编码是 utf-8,默认状态码是 200 等。实例化的时候接受的参数有:

  • response: 字符串或者其他 iterable 对象,作为响应的 body
  • status: 状态码,可以是整数,也可以是字符串
  • headers: 响应的头部,可以是个列表,也可以是 werkzeug.datastructures.Headers 对象
  • mimetype: mimetype 类型,告诉客户端响应 body 的格式,默认是文本格式
  • content_type: 响应头部的 Content-Type 内容

所有这些参数都是可选的,默认情况下会生成一个状态码为 200,没有任何 body 的响应。status、status_code 作为 Response 的属性,可以直接读取和修改。body 数据在内部保存为 iterable 的类型, 但是对外也提供了直接读写的接口 self.data

    def get_data(self, as_text=False):"""The string representation of the request body.  Whenever you callthis property the request iterable is encoded and flattened."""self._ensure_sequence()rv = b''.join(self.iter_encoded())if as_text:rv = rv.decode(self.charset)return rvdef set_data(self, value):"""Sets a new string as response.  The value set must either by aunicode or bytestring."""if isinstance(value, text_type):value = value.encode(self.charset)else:value = bytes(value)self.response = [value]if self.automatically_set_content_length:self.headers['Content-Length'] = str(len(value))data = property(get_data, set_data, doc='''A descriptor that calls :meth:`get_data` and :meth:`set_data`.  Thisshould not be used and will eventually get deprecated.''')

body 字符的编码和长度都是自动设置的,用户不需要手动处理。

至于头部的存储,werkzeug 使用的是类似于字典的 werkzeug.datastructures:Headers 类。在flask 源码解析:请求这篇文章中,我们没有详细 解释头部的存储,那么这篇文章就具体分析一下吧。

Headers 这个类的提供了很多和字典相同的接口:keys、values、iterms,但是和字典的区别在于它保存的值是有序的,而且允许相同 key 的值存在。 为什么这么设计呢?因为着更符合 HTTP 头部的特性。先来看看有序,在 HTTP 传送的过程中,如果头部各个 key-value 键值对顺序发生变化,有些代理或者客户端等组件会认为请求被篡改而丢弃或者拒绝请求的处理,所以最好把头部设置为有序的,用户按照什么顺序设置的,就按照什么顺序存储;再说说相同 key 的问题,这是因为 HTTP 头部同一个 key 可能有多个 value(比如 Accept、SetCookie头部)。那么这个看起比较特殊的字典是怎么实现的呢?来看代码:

class Headers(object):"""An object that stores some headers.  It has a dict-like interfacebut is ordered and can store the same keys multiple times."""def __init__(self, defaults=None):self._list = []if defaults is not None:if isinstance(defaults, (list, Headers)):self._list.extend(defaults)else:self.extend(defaults)def __getitem__(self, key, _get_mode=False):if not _get_mode:if isinstance(key, integer_types):return self._list[key]elif isinstance(key, slice):return self.__class__(self._list[key])if not isinstance(key, string_types):raise exceptions.BadRequestKeyError(key)ikey = key.lower()for k, v in self._list:if k.lower() == ikey:return vif _get_mode:raise KeyError()raise exceptions.BadRequestKeyError(key)

可以看到,头部信息在内部存储为二元组构成的列表,这样就能同时保证它的有序性和重复性。一个核心的方法是 __getitem__,它定义了如何获取头部中的信息:

  • 通过下标 header[3],直接返回对应未知存储的键值对元组
  • 通过 key,返回 value header['Accept'],返回匹配的第一个 value 值
  • 通过 slice header[3:7],返回另外一个 Headers 对象,保存了 slice 中所有的数据

然后实现 keys()items()pop()setdefault() 等方法让它表现出来字典的特性,除此之外还有 add()extend()add_header() 等和字典无关的方法方便操作。

自定义 response

如果需要扩展 flask Response 的功能,或者干脆把它替换掉,只要修改 flask app 的 response_class 属性就可以了,比如:

from flask import Flask, Responseclass MyResponse(Response):passapp = Flask(__name__)
app.response_class = MyResponse

更多可能的用法,可以参考文章末尾的参考资料。

参考资料

  • customizing the flask response class

这篇关于flask 源码解析:响应的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130016

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操