在 Apache2 中执行 Python 脚本 [英] Executing a Python script in Apache2

查看:38
本文介绍了在 Apache2 中执行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache 执行 Python 程序.但是,Apache 只会提供该文件,而不会实际执行它.该文件的权限为 r/w/x,位于 /var/www 中.httpd.conf 的内容和后面的程序代码我会贴出来.我还尝试将 python 脚本作为 .cgi 文件运行,但效果不佳.我也将 mod_pythonmod_wsgi 模块加载到 apache 中.

I am trying to execute a Python program using Apache. However, Apache will only serve the file and not actually execute it. The permissions on the file are r/w/x and it is in /var/www. I will post the contents of httpd.conf and the program code after. I also tried to running the python script as a .cgi file but that did not work as well. I have both the mod_python and mod_wsgi modules loaded into apache as well.

Python 示例:

#!/usr/bin/python

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain\r\n\r\n"
print

print "Hello World!"

httpd.conf:

httpd.conf:

AddHandler cgi-script .cgi .pl
AddHandler python-program .py

我知道它是一个小的 httpd.conf 文件,但是当我安装 apache 时,文件中没有任何内容.我还应该提到,这只是为了让我学习在 apache 中运行 python 的基础知识.它不适用于生产.感谢您的帮助!

I know its a small httpd.conf file but when I installed apache, there was nothing in the file. I should also mention that this is just so that I learn the basics of running python in apache. It is not meant for production. Thanks for the help!

我使用的操作系统是 Ubuntu 10.04,Apache 的版本是 2.我有 python 版本 2.6,它在调用 #!/usr/bin/env python 时自动使用.
我收到两个主要错误,第一个是即使文件和文件夹的权限为 777 也找不到文件.日志中的错误是

The OS I'm using is Ubuntu 10.04 and the version of apache is 2. I have python version 2.6 which is automatically used when #!/usr/bin/env python is invoked.
I am getting two main errors, the first is that the file is not being found even though the permissions of the file and folder are 777. The error from the log is

[Sun Feb 05 13:29:44 2012] [error] [client 192.168.1.3] File does not exist: /var/www/poit-0.1

此错误是针对我未编写的其他 Python 脚本造成的.奇怪的是,当从浏览器窗口访问时,该文件显示在文件夹的索引中.但是,当我导航到该文件时,出现上述错误.

This error is for a different python script that I did not write. What is weird is that the file shows up in the index of the folder when accessed from a browser window. However, when I navigate to the file, I get the above error.

我遇到的另一个错误是标题过早结束.错误如下:

The other error that I am getting is premature end of headers. The error is below:

[Sun Feb 05 12:10:19 2012] [error] (8)Exec format error: exec of '/var/www/pyth.py' failed
[Sun Feb 05 12:10:19 2012] [error] [client 192.168.1.3] Premature end of script headers: pyth.py

推荐答案

httpd.conf 的第一行:AddHandler cgi-script .cgi .pl 无关紧要,因为您正在测试 python 脚本而不是 perl 脚本.你应该在你的python脚本的位置定义这些指令,并告诉apache它应该在那个位置执行cgi脚本:Options +ExecCGI.这个片段将是一个开始:

The first line of httpd.conf: AddHandler cgi-script .cgi .pl is irrelevant, since you're testing python scripts and not perl scripts. And you should define those directives within the location of your python script, and tell apache that it should execute cgi scripts in that location: Options +ExecCGI. This snippet would be a start:

<Directory /path/to/sample.py />
  Options +ExecCGI
  AddHandler cgi-script .py
</Directory>

附录 1:

根据我上次的评论,试试这个脚本.它应该吐出有关 cgi 环境的信息.

As per my last comment, try this script. It should spit information about the cgi environment.

#!/usr/bin/python
import cgi
cgi.test()

附录 2:

我让你的脚本与上述配置一起工作.问题是脚本是用python2编写的.并且默认解释器 apache 正在调用以执行脚本,是 python3(至少在我的情况下,这对你来说也是一样的).

I got your script to work with the above configuration. The problem is that script is written in python2. And the default interpreter apache is invoking to execute the script, is python3 (at least in my case, and chances are this would be the same for you too).

这是 hello world 脚本的 python3 版本:

This is a python3 version of the hello world script:

#!/usr/bin/env python

# enable debugging
import cgitb
cgitb.enable()

print("Content-Type: text/plain;charset=utf-8")
print()

print("Hello World!")

附录 3:

对于第一个错误,请确保正确设置了您尝试部署的任何目录和文件的权限和所有权.并尝试将这些指令添加到 httpd.conf:

For the first error, make sure the permission and the ownership of whatever directory and files you're attempting to deploy are properly set. And try adding those directives to httpd.conf:

Order allow,deny
Allow from all

这会让你得到这个:

<Directory /path/to/sample.py />
  Options +ExecCGI
  AddHandler cgi-script .py
  Order allow,deny
  Allow from all
</Directory>

对于第二个错误,除非我遗漏了什么,看起来 apache 正在调用 python 3 解释器来执行您的脚本.要排除这种可能性,您可以尝试以下操作:

For the second error, unless I am missing something, it looks like apache is invoking python 3 interpreter to execute your script. To rule out this possibility, you might try the following:

ls -al /usr/bin/python*

这将列出您系统上可用的 python 解释器.如果您有多个解释器,您将得到与此类似的输出:

This will list the python interpreters available on your system. If you have more than one interpreter you'll get something similar to this output:

/usr/bin/python -> python3*
/usr/bin/python2.6*  
/usr/bin/python3*  

如果不是,就是这个输出:

If not, it would be this output:

/usr/bin/python -> python2.6*
/usr/bin/python2.6*  

为确保这不是您遇到的问题,请尝试使用此修改后的示例脚本:

To make sure, this is not the issue you're having, try with this modified sample script:

#!/usr/bin/python2.6

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain\r\n\r\n"
print

print "Hello World!"

您会注意到我明确提到了 apache 应该调用的解释器版本,这很丑陋.但是为了测试,你可以这样做.当然,您应该将 #!/usr/bin/python2.6 映射到服务器上的任何二进制文件,并确保不要将 python 3 可兼容代码与 python 2 解释器和副反之.

You'll notice that I explicitly mentioned the version of the interpreter apache should invoke, which is ugly. But for the sake of testing, you can do it. Of course you should map #!/usr/bin/python2.6, to whatever binary you have on your server, and make sure you don't mix python 3 comtipable code with python 2 interpreter and vice versa.

这篇关于在 Apache2 中执行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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