深刻理解 Python WSGI:一块儿写一个 Web 服务器

导读:html

本系列深刻浅出的讲述了如何用 Python 从 0 开始,写一个 web 服务器,并让其与业界流行的 web 框架协同工做,最后还进一步完善了开头的 web 服务器 demo,让其能够支持多并发请求的处理,并解决了过程中遇到的“僵尸进程”等一系列 socket/网络编程 中的常见问题,图文并茂、按部就班,是篇很是不错的教程,对了解整个 Web 编程理论至关有帮助,推荐一看。python

做者:伯乐在线 - 高世界 翻译nginx

一、什么是 Web 服务器,以及怎样工做的?

看问题要看到本质:从Web服务器提及web

http://bit.ly/2N1iiTsapache

一块儿写一个 Web 服务器(1)django

http://python.jobbole.com/81524/编程

Let’s Build A Web Server. Part 1.flask

http://ruslanspivak.com/lsbaws-part1/segmentfault

二、Web 服务器和 Web 框架如何经过 WSGI 协同工做?

一块儿写一个 Web 服务器(2)服务器

http://python.jobbole.com/81523/

Let’s Build A Web Server. Part 2.

http://ruslanspivak.com/lsbaws-part2/

花了两个星期,我终于把 WSGI 整明白了

https://zhuanlan.zhihu.com/p/68676316

三、Web 服务器如何处理并发请求?

一块儿写一个Web服务器(3)

http://python.jobbole.com/81820/

Let’s Build A Web Server. Part 3.

http://ruslanspivak.com/lsbaws-part3/

四、从0到1,Python Web开发的进击之路

https://zhuanlan.zhihu.com/p/25038203

五、flask 源码解析:应用启动流程

http://bit.ly/2n1fyaT

全部的 python web 框架都要遵循 WSGI 协议,若是对 WSGI 不清楚,能够查看《python wsgi 简介》。

在这里仍是要简单回顾一下 WSGI 的核心概念。

WSGI 中有一个很是重要的概念:每一个 python web 应用都是一个可调用(callable)的对象。在 flask 中,这个对象就是 app = Flask(__name__) 建立出来的 app,就是下图中的绿色 Application 部分。要运行 web 应用,必须有 web server,好比咱们熟悉的 apache、nginx ,或者 python 中的 gunicorn ,咱们下面要讲到的 werkzeug 提供的 WSGIServer,它们是下图的黄色 Server 部分。

The Python WSGI server-application interface.

图片来源(https://www.toptal.com/python/pythons-wsgi-server-application-interface)

Server 和 Application 之间怎么通讯,就是 WSGI 的功能。它规定了 app(environ, start_response) 的接口,server 会调用 application,并传给它两个参数:environ 包含了请求的全部信息,start_response 是 application 处理完以后须要调用的函数,参数是状态码、响应头部还有错误信息。

WSGI application 很是重要的特色是:它是能够嵌套的。换句话说,我能够写个 application,它作的事情就是调用另一个 application,而后再返回(相似一个 proxy)。通常来讲,嵌套的最后一层是业务应用,中间就是 middleware。这样的好处是,能够解耦业务逻辑和其余功能,好比限流、认证、序列化等都实现成不一样的中间层,不一样的中间层和业务逻辑是不相关的,能够独立维护;并且用户也能够动态地组合不一样的中间层来知足不一样的需求。

六、推荐阅读

[1] Python HOWTO 官方文档:Socket 编程

http://python.jobbole.com/81860/

[2] Python Realtime

http://pyzh.readthedocs.org/en/latest/python-realtime.html

[3] 做为一个python开发者须要知道的关于服务器的知识

http://python.jobbole.com/82009/

[4] 理解Python WSGI

http://www.letiantian.me/2015-09-10-understand-python-wsgi/

[5] 专题:浅入浅出Flask框架

http://www.letiantian.me/topic-learn-flask/

[6] 用Python写一个简单的Web框架

http://python.jobbole.com/83817/

[7] 深刻理解异步Web服务器 Tornado

http://python.jobbole.com/83826/

[8] flask route设计思路

http://segmentfault.com/a/1190000004213652

[9] Python微框架Flask源码剖析

http://bit.ly/2c7XvXO

[10] flask 源码解析:应用启动流程

http://bit.ly/2n1fyaT

 

五、关于 wsgi 问题集锦

问题:nginx<--->fstcgi<--->wsgi(flup)<--->web.py/Flask/Django这个模式是nginx和python的web网页的工做模式吗?

    Flup 一般不是生产环境选择,Gunicorn,uwsgi + nginx 和 Tornado 是几种常见方案吧。

[1] nginx<--->fstcgi<--->wsgi(flup)<--->web.py这个模式是nginx和python的web网页的工做模式吗?

http://www.zhihu.com/question/20221856

[2] 全面解读python web 程序的9种部署方式

http://lutaf.com/141.htm

[3] 深刻理解 Python WSGI fastcgi

http://www.cnblogs.com/babykick/archive/2012/01/20/2328047.html

[4] 在Mac上使用Nginx和FastCGI部署Flask应用

    http://segmentfault.com/a/1190000002652109

    nginx上用fastcgi配置python环境(一)  

    http://blog.163.com/sky20081816@126/blog/static/1647610232010824262695/

[5] 基于nginx和uWSGI在Ubuntu上部署Django

    http://www.jianshu.com/p/e6ff4a28ab5a

    快速部署Python应用:Nginx+uWSGI配置详解

    http://developer.51cto.com/art/201010/229615_all.htm

[6] 高性能框架gevent和gunicorn在web上的应用及性能测试

    http://rfyiamcool.blog.51cto.com/1030776/1276364

    Gunicorn快速入门

    http://www.cnblogs.com/ArtsCrafts/p/gunicorn.html

[7] gunicorn, uwsgi, fast-cgi, tornado 等等,你们通常都用哪一个作 server ?

http://python-china.org/t/100

[8] 在生产系统使用Tornado WebServer来代替FastCGI加速你的Django应用

    http://www.cnblogs.com/Alexander-Lee/archive/2011/05/02/tornado_host_django.html

    Tornado 中文文档

    http://www.tornadoweb.cn/documentation

相关文章
相关标签/搜索