[英]How to let the webserver (e.g. Apache) call Python directly?
(Important) Disclaimer: I know it's probably not a good idea, that Python is not like PHP, and that the "natural" way to do web with Python is more by using a framework like Bottle, Flask, Django (that I already use), etc. But still, just out of curiosity, I'd like to see how the following is possible . (Important) Disclaimer: I know it's probably not a good idea, that Python is not like PHP, and that the "natural" way to do web with Python is more by using a framework like Bottle, Flask, Django (that I already use ) 等。但是,出于好奇,我还是想看看以下是如何可能的。
When Apache + PHP are installed, we can access a page like http://www.example.com/index.php
.安装 Apache + PHP 后,我们可以访问像
http://www.example.com/index.php
这样的页面Internally, Apache probably passes the request to PHP which executes code, produces a text output, which is then served by Apache. Internally, Apache probably passes the request to PHP which executes code, produces a text output, which is then served by Apache.
Question: how could we do something similar in Python?问题:我们如何在 Python 中做类似的事情? ie by accessing
http://www.example.com/index.py
, Apache would call the script index.py
:即通过访问
http://www.example.com/index.py
, Apache 将调用脚本index.py
:
print("<html><body>Hello world</body></html>")
and then Apache would serve this page to the client.然后 Apache 会将此页面提供给客户端。
NB:注意:
Calling http://www.example.com/index.py?foo=bar
could even give the params to the Python script in sys.argv
调用
http://www.example.com/index.py?foo=bar
甚至可以将参数提供给sys.argv
中的 Python 脚本
I already did it like this: http://www.example.com/index.php
:我已经这样做了:
http://www.example.com/index.php
:
<?php $out = shell_exec("python index.py"); echo($out); ?>
which then calls the Python script and produces the output.然后调用 Python 脚本并生成 output。 It works, but I'd like to do it without PHP.
它有效,但我想在没有 PHP 的情况下这样做。
Said in another way, is there something like mod_php
for Python?换句话说,Python 有类似
mod_php
的东西吗?
There exists a similar mod for python, but it's not as widely used and does not seem to have been updated for a few years. python 有一个类似的 mod ,但它没有被广泛使用,而且似乎几年没有更新。
Note: A common way of doing things is using apache/nginx as a web server, and uwsgi as an application server, with the web server redirecting to the application server for non-static content urls.注意:一种常见的处理方式是使用 apache/nginx 作为 web 服务器,使用 uwsgi 作为应用程序服务器,web 服务器重定向到应用程序服务器以获取非静态内容 url。
I finally managed to do it thanks to the other answer:由于另一个答案,我终于设法做到了:
Do:做:
apt-get install libapache2-mod-python
Then create or open the .htaccess
file in your website folder, and add然后在您的网站文件夹中创建或打开
.htaccess
文件,并添加
AddHandler mod_python.py PythonHandler mod_python.publisher
Then create a file test.py
:然后创建一个文件
test.py
:
def index(req): return("<html><body>Hello world</body></html>")
Now accessing www.example.com/test.py
works!现在访问
www.example.com/test.py
工作!
NB:注意:
def index(req)
is really required: using another name will make it fail. def index(req)
确实是必需的:使用另一个名称会使其失败。
I don't know why, but it's not possible to set AddHandler mod_python.py
in a .htaccess
, I only managed to do it globally for a <VirtualHost>
.我不知道为什么,但是不可能在
.htaccess
中设置AddHandler mod_python.py
,我只设法在全局范围内为<VirtualHost>
进行设置。 Does someone have an idea about how to do it directly in the .htaccess
?有人知道如何直接在
.htaccess
中进行操作吗?
if mod_python
is already installed but not enabled, you have to do:如果
mod_python
已安装但未启用,则必须执行以下操作:
a2enmod python service apache2 restart
but this is done automatically when installing libapache2-mod-python
.但这是在安装
libapache2-mod-python
时自动完成的。
This is needed in the Apache VirtualHost
's Directory
: AllowOverride All
, Require all granted
, to allow handlers to be added directly in a .htaccess
file.这在 Apache
VirtualHost
的Directory
中是必需的: AllowOverride All
, Require all granted
allgranted ,以允许将处理程序直接添加到.htaccess
文件中。 Without it, an alternative is to add the directives AddHandler...
directly in the VirtualHost
definition.如果没有它,另一种方法是直接在
VirtualHost
定义中添加指令AddHandler...
There is one more module apache can able to server python apart from mod_python
which is mod_wsgi
you may have tried it already if not this can be done like below.还有一个模块 apache 可以服务器 python 除了
mod_python
是mod_wsgi
你可能已经尝试过,如果不是,可以像下面这样完成。
First install if not already installed如果尚未安装,请先安装
sudo apt-get install libapache2-mod-wsgi -y
Create vhost创建虚拟主机
<VirtualHost *:8081>
#ServerName www.example.com
#ServerAlias example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/wsgi
<Directory /var/www/html/wsgi>
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
WSGIScriptAlias /myapp /var/www/html/wsgi/index.py #path to file
<Directory /var/www/html/wsgi>
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
</VirtualHost>
Create index.py创建 index.py
def application(environ, start_response):
status = '200 OK'
output = b'<h2>Hello World!</h2>'
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.