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

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

问题描述

我想执行使用Apache Python程序。但是,Apache将只会文件并没有真正执行。该文件的权限是R / W / X,它是在 /无功/网络。我将发布的httpd.conf 的内容和之后的节目code。我也试着运行python脚本作为的CGI 文件,但没有正常工作。我有两个的mod_python 的mod_wsgi 模块加载到Apache的为好。

Python示例:

 #!的/ usr / bin中/蟒蛇#启用调试
进口cgitb
cgitb.enable()打印内容类型:纯文本/ \\ r \\ n \\ r \\ n
打印打印的Hello World!

httpd.conf中:

  AddHandler的CGI脚本的CGI特等
AddHandler的蟒蛇程序的.py

我知道它的一个小httpd.conf文件,但是当我安装了Apache,没有任何的文件中。我还要提到的是,这只是让我学会在Apache上运行的Python的基本知识。这并不意味着生产。
感谢您的帮助!

修改

我使用的操作系统是Ubuntu的10.04和Apache的版本是2。我有Python版本2.6是自动使用时#!的/ usr /斌/包膜蟒蛇被调用。结果
我得到两个主要错误,第一个是文件不被发现,即使文件和文件夹的权限是777从日志中的误差

  [太阳2月5日13时29分44秒2012] [错误] [客户端192.168.1.3]文件不存在:/var/www/poit-0.1

此错误是不同的Python脚本,我没有写。什么是奇怪的是,在该文件中的文件夹的索引从浏览器窗口进行访问时显示出来。但是,当我浏览到该文件,我得到上述错误。

这是我收到的其他错误是头premature结束。下面的错误是:

 【孙二月五日十二点10分19秒2012] [错误](8)Exec的格式错误:的EXEC/var/www/pyth.py'失败
[太阳2月5日12时一十分19秒2012] [错误] [客户端192.168.1.3]脚本头premature结束:pyth.py


解决方案

httpd.conf文件的第一行: AddHandler的CGI脚本的CGI特等是无关紧要的,因为您正在测试的Python脚本,而不是Perl脚本。你应该在你的Python脚本的位置内定义的指令,告诉Apache的,它应该在的位置执行CGI脚本:选项+ ExecCGI 。这段代码是一个开始:

 <目录/path/to/sample.py />
  选项​​+ ExecCGI
  AddHandler的CGI脚本的.py
< /目录>

附录1

按我最后的评论,试试这个脚本。它应该吐出有关CGI环境信息。

 #!的/ usr / bin中/蟒蛇
进口CGI
cgi.test()

附录2

我收到了你的脚本与上面的配置工作。问题是,脚本写入python2。并且默认间preTER Apache是​​调用执行脚本,是python3(至少在我的情况下,机会是这将是你同样也是如此)。

这是世界您好脚本的python3版本:

 #!的/ usr /斌/包膜蟒蛇#启用调试
进口cgitb
cgitb.enable()打印(内容类型:text / plain的;字符集= UTF-8)
打印()打印(的Hello World!)

附录3

对于第一个错误,确保许可,任何目录和文件,你尝试部署设置正确的所有权。然后尝试在这些指令到httpd.conf文件:

 令允许,拒绝
所有允许

这将让你这样的:

 <目录/path/to/sample.py />
  选项​​+ ExecCGI
  AddHandler的CGI脚本的.py
  为了允许,拒绝
  所有允许
< /目录>

对于第二个错误,除非我失去了一些东西,它看起来像Apache是​​调用蟒蛇3间preTER来执行你的脚本。为了排除这种可能性,你可以尝试以下操作:

  LS -al的/ usr / bin中/ Python的*

这将会列出你的系统上可用的蟒蛇间preters。如果你有一个以上的跨preTER你会得到类似这样的输出:

 的/ usr / bin中/ Python的 - > python3 *
/usr/bin/python2.6*
在/ usr /斌/ python3 *

如果不是,那将是这样的输出:

 的/ usr / bin中/ Python的 - > python2.6的*
/usr/bin/python2.6*

要确保,这是不是您遇到的问题,请尝试用这个修改示例脚本:

 #!的/ usr / bin中/ python2.6的#启用调试
进口cgitb
cgitb.enable()打印内容类型:纯文本/ \\ r \\ n \\ r \\ n
打印打印的Hello World!

您会发现,我明确提到跨preTER阿帕奇应该调用,这是丑陋的版本。但是出于测试的目的,你可以做到这一点。当然,你应该映射#!的/ usr / bin中/ python2.6的,你有你的服务器上的任何二进制文件,并确保你不混合蟒蛇3 comtipable code。与蟒蛇2间preTER,反之亦然。

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 sample:

#!/usr/bin/python

# enable debugging
import cgitb
cgitb.enable()

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

print "Hello World!"

httpd.conf:

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

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!

Edit

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

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

解决方案

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>

Addendum 1:

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

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

Addendum 2:

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).

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!")

Addendum 3:

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

Which will get you this:

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

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*

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!"

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天全站免登陆