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

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

问题描述

我有一个 BlueHost的占在那里我可以作为CGI运行Python脚本。我想这是最简单的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的网络编程,我听到了很多有关WSGI以及如何大多数的框架使用它。但我只是不明白这一切是如何结合在一起,尤其是给予我的Web服务器时(阿帕奇在主机的机器上运行)和不是我可以真的(除了玩定义的.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.py 或的 CherryPy的)对我的基本的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请求。它解析为找到一种方式来响应请求。阿帕奇有应对的选择很多。响应的一种方法是使用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的情况下,阿帕奇prepares的环境,并通过CGI协议调用脚本。这是一个标准的Unix叉/ Exec的情况 - 在CGI子继承一个操作系统环境,包括插座和stdout。该CGI子写的回应,可以追溯到到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是原始的和讨厌。主要是因为它派生一个子进程为每个请求,和子进程必须退出或关闭输出和错误来表示响应的结束。

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的)?

回想一下,分叉一子是昂贵的。有两种方法来解决这个问题。

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


  1. 嵌入式 的mod_wsgi 的mod_python 嵌入Python的Apache内部;没有过程分叉。阿帕奇直接运行Django应用程序。

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

守护程序 的mod_wsgi 的mod_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 创建一个WSGI兼容接口由的mod_fastcgi提供的信息。该管道是这样的。

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支持?

按照以下说明。

<一个href=\"http://$c$c.google.com/p/modwsgi/wiki/IntegrationWithDjango\">http://$c$c.google.com/p/modwsgi/wiki/IntegrationWithDjango

有关背景看到这个

<一个href=\"http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index\">http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index

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

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