像 CGI PHP 一样配置 Apache 使用 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/799354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:31:59  来源:igfitidea点击:

Configure Apache to use Python just like CGI PHP

pythonapachecgi

提问by okoman

I think one commonly known way of adding PHP to an Apache webserver is to configure it like this:

我认为将 PHP 添加到 Apache 网络服务器的一种众所周知的方法是像这样配置它:

ScriptAlias /php5.3 /usr/local/php5.3/bin
Action application/php5.3 /php5.3/php-cgi
AddType application/php5.3 .php

Now I tried to write a similar configuration for Python:

现在我尝试为 Python 编写一个类似的配置:

ScriptAlias /python /usr/bin
Action application/python /python/python
AddType application/python .py

I have a small test script that looks like this:

我有一个看起来像这样的小测试脚本:

print "Content-Type: text/html\n\n"
print "Test"

But something seems to be wrong since the apache error log says the following:

但是似乎有些错误,因为 apache 错误日志显示如下:

Premature end of script headers: python

So my first though was that my python response is not right. But there is the Content-Type and also both linebreaks. Also the output of a similar PHP script called with php-cgigives exactly the same output.

所以我的第一个想法是我的 python 响应不正确。但是有 Content-Type 和两个换行符。调用 with 的类似 PHP 脚本的输出也php-cgi提供完全相同的输出。

Also I haven't found a tutorial that shows how to get python working this way. So maybe it is not possible, but then I'm curious why this is the case? Or am I missing something?

此外,我还没有找到展示如何让 python 以这种方式工作的教程。所以也许这是不可能的,但我很好奇为什么会这样?或者我错过了什么?

回答by JimB

You can use anytype of executable as cgi. Your problem is in your apache config, which looks like you just made it up. Check the apache docs for more details, but you don't need the Action and AddType.

您可以使用任何类型的可执行文件作为 cgi。您的问题出在您的 apache 配置中,看起来像是您刚刚编造的。查看 apache 文档以获取更多详细信息,但您不需要 Action 和 AddType。

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

Then drop the following into your cgi-bin:

然后将以下内容放入您的 cgi-bin:

#!/usr/bin/python
# test.py
print "Content-Type: text/html\n\n"
print "Test"

Make sure it's executable, and see the result at /cgi-bin/test.py

确保它是可执行的,并在 /cgi-bin/test.py 查看结果

回答by S.Lott

" So maybe it is not possible, but then I'm curious why this is the case?"

“所以也许这是不可能的,但我很好奇为什么会这样?”

Correct. It's not possible. It was never intended, either.

正确的。这是不可能的。它也从来没有打算。

Reason 1 - Python is not PHP. PHP -- as a whole -- expects to be a CGI. Python does not.

原因 1 - Python 不是 PHP。PHP——作为一个整体——期望成为一个 CGI。Python 没有。

Reason 2 - Python is not inherently a CGI. It's an interpreter that has (almost) no environmental expectations.

原因 2 - Python 本质上不是 CGI。这是一个(几乎)没有环境期望的口译员。

Reason 3 - Python was never designed to be a CGI. That's why Python is generally embedded into small wrappers (mod_python, mod_wsgi, mod_fastcgi) which can encapsulate the CGI environment in a form that makes more sense to a running Python program.

原因 3 - Python 从未被设计为 CGI。这就是为什么通常将 Python 嵌入到小型包装器(mod_python、mod_wsgi、mod_fastcgi)中的原因,这些包装器可以以对运行的 Python 程序更有意义的形式封装 CGI 环境。

回答by Damion Hart

The error "Premature end of script headers:" can occur if the .py file was edited in a Windows program that uses CRLF characters for line breaks instead of the Unix LF.

如果 .py 文件是在使用 CRLF 字符而不是 Unix LF 换行的 Windows 程序中编辑的,则可能会出现错误“脚本标题过早结束:”。

Some programs like Dreamweaver have line-break-type in preferences. Notepad also uses CRLF.

某些程序(如 Dreamweaver)在首选项中具有换行符类型。记事本也使用 CRLF。

If your host has a file editor, you could test by backspacing the current linebreaks and reentering them through that editor which would change any CRLF to LF only. Notepad++ can use LF only.

如果您的主机有文件编辑器,您可以通过将当前换行符退格并通过该编辑器重新输入它们来进行测试,该编辑器只会将任何 CRLF 更改为 LF。Notepad++ 只能使用 LF。

回答by Cleric

When you open for example http://localhost/test.pyyou expect that Apache will somehow start process /usr/bin/python /var/www/test.py(i.e the interpreter with single command line argument). But this is not what happens because Apache calls the cgi script with noarguments. Instead it provides all the information through environment variables which are standardized by CGI.

例如,当您打开时,http://localhost/test.py您希望 Apache 以某种方式启动进程/usr/bin/python /var/www/test.py(即具有单个命令行参数的解释器)。但这不会发生,因为 Apache不带参数调用 cgi 脚本。相反,它通过由 CGI 标准化的环境变量提供所有信息。

As others have pointed out using python as plain cgi is inefficient but if for educational reasons you would still like to do it you can try this.

正如其他人指出的那样,使用 python 作为普通的 cgi 效率低下,但如果出于教育原因,您仍然想这样做,您可以尝试这样做。

Assuming that the default Apache cgi-bin settings are active you can create a simple wrapper named python(or whatever you choose) in your /usr/lib/cgi-binwith the following contents:

假设默认的 Apache cgi-bin 设置处于活动状态,您可以使用以下内容创建一个名为python(或您选择的任何名称)的简单包装器/usr/lib/cgi-bin

#!/usr/bin/python
import os
execfile(os.environ['PATH_TRANSLATED'])

Don't forget to make it executable: chmod a+x /usr/lib/cgi-bin/python

不要忘记使其可执行: chmod a+x /usr/lib/cgi-bin/python

Put these in you Apache config:

将这些放在你的 Apache 配置中:

AddType application/python .py
Action application/python /cgi-bin/python

Now when you open http://localhost/test.pyApache will execute /cgi-bin/python with no arguments but with filled in CGI environment variables. In this case we use PATH_TRANSLATEDsince it points directly to the file in the webroot.
Calling execfileinterprets that script inside the already opened python process.

现在,当您打开http://localhost/test.pyApache 时,将执行 /cgi-bin/python 不带参数但填充 CGI 环境变量。在这种情况下我们使用PATH_TRANSLATED因为它直接指向 webroot 中的文件。
调用execfile在已经打开的 python 进程中解释该脚本。