Python Web 框架、WSGI 和 CGI​​ 如何结合在一起 [英] How Python web frameworks, WSGI and CGI fit together

查看:22
本文介绍了Python Web 框架、WSGI 和 CGI​​ 如何结合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Bluehost 帐户,我可以在其中将 Python 脚本作为 CGI 运行.我想这是最简单的 CGI,因为要运行我必须在 .htaccess 中定义以下内容:

I have a Bluehost account where I can run Python scripts as CGI. I guess it's the simplest CGI, because to run I have to define the following in .htaccess:

Options +ExecCGI
AddType text/html py
AddHandler cgi-script .py

现在,每当我使用 Python 查找 Web 编程时,我都会听到很多关于 WSGI 以及大多数框架如何使用它的信息.但我只是不明白它们是如何组合在一起的,尤其是当我的网络服务器被提供(Apache 在主机的机器上运行)而不是我真正可以玩的东西时(除了定义 .htaccess 命令).

Now, whenever I look up web programming with Python, I hear a lot about WSGI and how most frameworks use it. But I just don't understand how it all fits together, especially when my web server is given (Apache running at a host's machine) and not something I can really play with (except defining .htaccess commands).

WSGI、CGI 和框架是如何联系在一起的?如果我想运行一个 Web 框架(比如 web.pyCherryPy) 在我的基本 CGI 配置中?如何安装 WSGI 支持?

How are WSGI, CGI, and the frameworks all connected? What do I need to know, install, and do if I want to run a web framework (say web.py or CherryPy) on my basic CGI configuration? How to install WSGI support?

推荐答案

WSGI、CGI 和框架是如何连接起来的?

Apache 侦听端口 80.它收到一个 HTTP 请求.它解析请求以找到一种响应方式.Apache 有很多响应选择.一种响应方式是使用 CGI 来运行脚本.另一种响应方式是简单地提供一个文件.

Apache listens on port 80. It gets an HTTP request. It parses the request to find a way to respond. Apache has a LOT of choices for responding. One way to respond is to use CGI to run a script. Another way to respond is to simply serve a file.

在CGI的情况下,Apache准备环境并通过CGI协议调用脚本.这是一个标准的 Unix Fork/Exec 情况——CGI 子进程继承了一个操作系统环境,包括套接字和标准输出.CGI 子进程写入一个响应,返回给 Apache;Apache 将此响应发送到浏览器.

In the case of CGI, Apache prepares an environment and invokes the script through the CGI protocol. This is a standard Unix Fork/Exec situation -- the CGI subprocess inherits an OS environment including the socket and stdout. The CGI subprocess writes a response, which goes back to Apache; Apache sends this response to the browser.

CGI 很原始而且很烦人.主要是因为它为每个请求派生一个子进程,并且子进程必须退出或关闭 stdout 和 stderr 以表示响应结束.

CGI is primitive and annoying. Mostly because it forks a subprocess for every request, and subprocess must exit or close stdout and stderr to signify end of response.

WSGI 是一个基于 CGI 设计模式的接口.它不一定是 CGI——它不必为每个请求创建一个子进程.它可以是 CGI,但不一定是.

WSGI is an interface that is based on the CGI design pattern. It is not necessarily CGI -- it does not have to fork a subprocess for each request. It can be CGI, but it doesn't have to be.

WSGI 以几种重要的方式添加到 CGI 设计模式中.它为您解析 HTTP 请求标头并将它们添加到环境中.它将任何面向 POST 的输入作为环境中的类文件对象提供.它还为您提供了一个用于制定响应的功能,从而使您免于许多格式化细节.

WSGI adds to the CGI design pattern in several important ways. It parses the HTTP Request Headers for you and adds these to the environment. It supplies any POST-oriented input as a file-like object in the environment. It also provides you a function that will formulate the response, saving you from a lot of formatting details.

如果我想在基本 CGI 配置上运行 Web 框架(比如 web.py 或cherrypy),我需要知道/安装/做什么?

回想一下,fork 一个子进程是很昂贵的.有两种方法可以解决这个问题.

Recall that forking a subprocess is expensive. There are two ways to work around this.

  1. Embedded mod_wsgimod_python 将 Python 嵌入到 Apache 中;没有进程被分叉.Apache 直接运行 Django 应用程序.

  1. Embedded mod_wsgi or mod_python embeds Python inside Apache; no process is forked. Apache runs the Django application directly.

守护进程 mod_wsgimod_fastcgi 允许 Apache 与单独的守护进程(或长时间运行的进程")交互,使用WSGI 协议.您启动长时间运行的 Django 进程,然后配置 Apache 的 mod_fastcgi 以与该进程通信.

Daemon mod_wsgi or mod_fastcgi allows Apache to interact with a separate daemon (or "long-running process"), using the WSGI protocol. You start your long-running Django process, then you configure Apache's mod_fastcgi to communicate with this process.

请注意,mod_wsgi 可以在任一模式下工作:嵌入式或守护进程.

Note that mod_wsgi can work in either mode: embedded or daemon.

当您阅读 mod_fastcgi 时,您会看到 Django 使用 flup 来创建来自 mod_fastcgi 提供的信息的 WSGI 兼容接口.管道是这样工作的.

When you read up on mod_fastcgi, you'll see that Django uses flup to create a WSGI-compatible interface from the information provided by mod_fastcgi. The pipeline works like this.

Apache -> mod_fastcgi -> FLUP (via FastCGI protocol) -> Django (via WSGI protocol)

Django 为各种接口提供了几个django.core.handlers".

Django has several "django.core.handlers" for the various interfaces.

对于 mod_fastcgi,Django 提供了一个 manage.py runfcgi 集成了 FLUP 和处理程序.

For mod_fastcgi, Django provides a manage.py runfcgi that integrates FLUP and the handler.

对于 mod_wsgi,有一个核心处理程序.

For mod_wsgi, there's a core handler for this.

如何安装 WSGI 支持?

遵循这些说明.

https://code.google.com/archive/p/modwsgi/wikis/IntegrationWithDjango.wiki

背景看这个

http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index

这篇关于Python Web 框架、WSGI 和 CGI​​ 如何结合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆