简体   繁体   English

从Apache运行python脚本的最简单方法

[英]Easiest way to run python script from Apache

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. 我基本上是在尝试开发一个网站,当用户单击特定按钮时,我必须在该网站上执行python脚本。 After researching on Stack Overflow and Google, I need to configure Apache to be able to run CGI scripts. 在研究了Stack Overflow和Google之后,我需要配置Apache以能够运行CGI脚本。 I have seen numerous examples of software that can accomplish this, such as mod_wsgi . 我已经看到了许多可以实现此目的的软件示例,例如mod_wsgi The problem is that I get extremely confused with the instructions for these softwares, especially for mod_wsgi . 问题是我对这些软件的说明非常困惑,尤其是对于mod_wsgi I don't understand the instructions at all, and I can't install the software and get anything to work. 我完全不了解其中的说明,也无法安装该软件并使任何工作正常进行。

If anyone has a very easy method for executing python scripts in Apache, I would greatly appreciate. 如果有人有一个非常简单的方法来在Apache中执行python脚本,我将不胜感激。 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. 如果有人想解释如何使用mod_wsgi,我也将不胜感激,因为到目前为止,我还不知道如何使用它,并且安装说明使我感到困惑。

One way, not so easy but simple in some way, is to use CGI, as you said. 如您所说,使用CGI并不是那么容易,而是在某种程度上很简单。 You can find more information on Apache documentation and Python CGI module documentation . 您可以在Apache文档Python CGI模块文档中找到更多信息。

But, basically, you have to set your server to run cgi sripts. 但是,基本上,您必须将服务器设置为运行cgi sripts。 This is done by editing httpd.conf or a .htaccess file: For the first option, add or uncomment the follow: 这可以通过编辑httpd.conf或.htaccess文件来完成:对于第一个选项,请添加或取消注释以下内容:

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.

Then, you just need to add your python script in the directory that you set above. 然后,您只需要在上面设置的目录中添加python脚本即可。

Note that the output from your script must be preceded by a mime-type header, as Apache documentation says. 请注意,如Apache文档所述,脚本输出必须带有mime-type标头。

So, a hello world script could be named hello.py and its content could be: 因此,hello world脚本可以命名为hello.py,其内容可以是:

#!/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')

Than, you can call your scrit from a browser: 然后,您可以从浏览器调用scrit:

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

Note that Python has some goodies inside its cgi realted builtin modules. 请注意,Python在其cgi realized内置模块内部具有一些优点。 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. cgi模块将为您提供处理表单的方法,而cgitb将为您提供一种有用的(但不是完美的)调试脚本的方法。 For more on that, read the documentation again. 有关更多信息,请再次阅读文档。

Finally, using cgi directly to run python scripts gives you a raw way to work with http requests. 最后,直接使用cgi运行python脚本为您提供了处理http请求的原始方法。 There is a lot of already done frameworks, like flask and django, that gives you more power easily. 有许多已经完成的框架,例如flask和django,可以轻松为您提供更多功能。 You could check that. 您可以检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM