从Apache运行python脚本的最简单方法 [英] Easiest way to run python script from Apache

查看:118
本文介绍了从Apache运行python脚本的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很长时间试图弄清楚这一点.我基本上是在尝试开发一个网站,当用户单击特定按钮时,我必须在该网站上执行python脚本.在研究了Stack Overflow和Google之后,我需要配置Apache以能够运行CGI脚本.我已经看到许多可以实现此目的的软件示例,例如 mod_wsgi .问题是我对这些软件的说明非常困惑,尤其是对于 mod_wsgi .我完全不了解其中的说明,也无法安装该软件并使任何工作正常进行.

I have spent ages trying to figure this out. I'm basically trying to develop a website where I have to execute a python script when the users clicks a specific button. After researching on Stack Overflow and Google, I need to configure Apache to be able to run CGI scripts. I have seen numerous examples of software that can accomplish this, such as mod_wsgi. The problem is that I get extremely confused with the instructions for these softwares, especially for mod_wsgi. I don't understand the instructions at all, and I can't install the software and get anything to work.

如果任何人都有一个非常简单的在Apache中执行python脚本的方法,我将不胜感激.如果有人想解释如何使用mod_wsgi,我也将不胜感激,因为到目前为止,我还不知道如何使用它,并且安装说明使我感到困惑.

If anyone has a very easy method for executing python scripts in Apache, I would greatly appreciate. If anyone would like to explain how to use mod_wsgi, I would greatly appreciate that as well, because as of right now I have no idea how to use it and the installation instructions are confusing the hell out of me.

推荐答案

一种方法,不是那么容易,但在某种程度上却很简单,就是使用CGI.您可以在 Apache文档

One way, not so easy but simple in some way, is to use CGI, as you said. You can find more information on Apache documentation and Python CGI module documentation.

但是,基本上,您必须将服务器设置为运行cgi sripts.这是通过编辑httpd.conf或.htaccess文件来完成的:对于第一个选项,请添加或取消注释以下内容:

But, basically, you have to set your server to run cgi sripts. This is done by editing httpd.conf or a .htaccess file: For the first option, add or uncomment the follow:

LoadModule cgi_module modules/mod_cgi.so

ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/ # update the second location according to your configuration. It has to be a place where apache is allow to use, otherwise see apache documentation for setting another directory.

然后,您只需要在上面设置的目录中添加python脚本即可.

Then, you just need to add your python script in the directory that you set above.

请注意,如Apache文档所述,脚本输出必须带有mime类型的标头.

Note that the output from your script must be preceded by a mime-type header, as Apache documentation says.

因此,一个hello world脚本可以命名为hello.py,其内容可以是:

So, a hello world script could be named hello.py and its content could be:

#!/usr/bin/python
print('Content-type: text/html') # the mime-type header.
print() # header must be separated from body by 1 empty line.
print('Hello world')

然后,您可以通过浏览器致电您的scrit:

Than, you can call your scrit from a browser:

http://localhost/cgi-bin/hello.py

请注意,Python在其cgi realized内置模块中具有一些优点.cgi模块将为您提供处理表单的方法,而cgitb将为您提供一种有用的(但不是完美的)调试脚本的方法.有关更多信息,请再次阅读文档.

Note that Python has some goodies inside its cgi realted builtin modules. The cgi module will give you a way to handle forms, and cgitb will give you a helpful (but not perfect) way to debug your script. For more on that, read the documentation again.

最后,直接使用cgi运行python脚本为您提供了处理http请求的原始方法.有很多已经完成的框架,例如flask和django,可以轻松为您提供更多功能.您可以检查一下.

Finally, using cgi directly to run python scripts gives you a raw way to work with http requests. There is a lot of already done frameworks, like flask and django, that gives you more power easily. You could check that.

这篇关于从Apache运行python脚本的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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