Django WSGI响应过程之WSGIHandler
class WSGIHandler(base.BaseHandler):
request_class = WSGIRequest def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.load_middleware() #载入中间件装饰器 def __call__(self, environ, start_response):#每次请求被调用
set_script_prefix(get_script_name(environ))
signals.request_started.send(sender=self.__class__, environ=environ)
request = self.request_class(environ) #获取WSGIrequest对象,保存header
response = self.get_response(request) #获取response对象,见下方 response._handler_class = self.__class__ status = '%d %s' % (response.status_code, response.reason_phrase) #准备返回状态
response_headers = [
*response.items(),
*(('Set-Cookie', c.output(header='')) for c in response.cookies.values()),
] #准备返回header
start_response(status, response_headers) #返回响应体
if getattr(response, 'file_to_stream', None) is not None and environ.get('wsgi.file_wrapper'):
response = environ['wsgi.file_wrapper'](response.file_to_stream)
return response
handlers/base中:
def get_response(self, request):
"""Return an HttpResponse object for the given HttpRequest."""
# Setup default url resolver for this thread
set_urlconf(settings.ROOT_URLCONF) #设置urls/base.py中的_urlconf.value
response = self._middleware_chain(request) #依次调用中间件后返回response
response._closable_objects.append(request)
if response.status_code >= 400: #异常处理
log_response(
'%s: %s', response.reason_phrase, request.path,
response=response,
request=request,
)
return response
Django WSGI响应过程之WSGIHandler的更多相关文章
- python web框架 django wsgi 理论
django wsgi python有个自带的wsgi模块 可以写自定义web框架 用wsgi在内部创建socket对象就可以了 自己只写处理函数就可以了django只是web框架 他也不负责写soc ...
- [TimLinux] django WSGI入口分析及自定义WSGIHandler思路
1. 命令行启动 命令行是通过runserver子命令来启动的,对应的django模块为django.core.management.commands.runserver,调用关系结构: # 简化的运 ...
- tornado和django的结合使用 tornado Server for django WSGI APP
#!/usr/bin/env python # Run this with # Serves by default at # http://localhost:8080/hello-tornado a ...
- Django WSGI,MVC,MTV,中间件部分,Form初识
一.什么是WSGI? WEB框架的本质是一个socket服务端接收用户请求,加工数据返回给客户端(Django),但是Django没有自带socket需要使用 别人的 socket配合Django才能 ...
- Django WSGI Error:class.__dict__ not accessible in restricted mode
一.问题 今天网站出了一个错误: RuntimeError at /index.html class.__dict__ not accessible in restricted mode 二.原因 用 ...
- django rest framwork教程之 viewsets和routers
ViewSets 和Routers REST框架包括一个用于抽象处理的ViewSets,允许开发人员集中精力对API的状态和交互进行建模,并根据常见约定自动处理URL构造. Viewset 类和 Vi ...
- Django请求响应对象
请求与响应对象 HttpRequest HttpRequest存储了客户请求的相关参数和一些查询方法. path 请求页面的全路径,不包括域名-例如, "/hello/". met ...
- django wsgi nginx 配置
""" WSGI config for HelloWorld project. It exposes the WSGI callable as a module-leve ...
- django返回响应对象
Django的视图必须要返回一个HttpResponse对象(或者其子类对象),不能像flask一样直接返回字符串. Django: return HttpResponse("Hello&q ...
随机推荐
- 配置文件一mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...
- 「题解」:$Game$
问题 B: $Game$ 时间限制: 1 Sec 内存限制: 256 MB 题面 题面谢绝公开. 题解 对于最初加入的每一个元素开桶记录出现次数. 然后记录一个前p个元素最大值. 先由先手玩家取走一 ...
- docker中国区镜像加速
[root@syzyy ~]# vim /etc/docker/daemon.json { "registry-mirros":[ "https://registry.d ...
- Linux课程---14、linux下lamp环境如何安装
Linux课程---14.linux下lamp环境如何安装 一.总结 一句话总结: 要按顺序安装,比如apache需要在php之前安装, 一.安装 gcc 编译器 二.卸载 rpm 安装的 http ...
- ORM(Object/Relation Mapping)框架简介
ORM 框架简介 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应用开发环境中的主流开发方法,关 ...
- mavlink 笔记1
Packet Anatomy This is the anatomy of one packet. It is inspired by the CAN and SAE AS-4 standards. ...
- HDU-1501-Zipper-字符串的dfs
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
- CentOS中GDB提示Missing separate debuginfos解决办法
安装debuginfo 修改文件 vi /etc/yum.repo.d/CentOS-Debuginfo.repo 修改enabled的值为1 使用debuginfo-install安装需要的文件
- Redis 的 4 大法宝,2018 必学中间件!
Redis是什么? 全称:REmote DIctionary Server Redis是一种key-value形式的NoSQL内存数据库,由ANSI C编写,遵守BSD协议.支持网络.可基于内存亦可持 ...
- 【bzoj 2870】 最长道路tree
题目 边分治 边分和点分相比就是找到一条重心边,考虑所有经过这条边的路径,之后断开这条边分成两个联通块,继续分治 由于每次分治重心是一条边,所以只会产生两个联通块,考虑两个联通块显然要比像点分那样考虑 ...