简体   繁体   English

如何让网络服务器(如Apache)直接调用Python?

[英]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:由于另一个答案,我终于设法做到了:

  1. Do:做:

     apt-get install libapache2-mod-python
  2. Then create or open the .htaccess file in your website folder, and add然后在您的网站文件夹中创建或打开.htaccess文件,并添加

    AddHandler mod_python.py PythonHandler mod_python.publisher
  3. Then create a file test.py :然后创建一个文件test.py

     def index(req): return("<html><body>Hello world</body></html>")
  4. 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 VirtualHostDirectory中是必需的: AllowOverride AllRequire 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_pythonmod_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.

相关问题 如何调用一个函数来重新加载另一个函数(例如在“ orientationchange()”上)? - How to call a function to reload another function (on “orientationchange()” e.g.)? 如何在PHP中循环$ num,例如1、2、3、4、1、2、3、4、1、2、3、4 - How to loop $num in php, e.g. 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, PHP整数配置(例如1 = apache,2 = php,3 = apache + php) - PHP Integer Configuration (e.g. 1=apache,2=php,3=apache+php) 如何突出显示包含可变字符(例如&#39;,%,$,数字)的文本 - how to highlight text containing variable character (e.g. ', %, $, numbers) 如何在数据库中添加每条最后一条记录? 例如1 + = 1 - How to add every last record on the database? E.g. 1 += 1 如何阻止来自我的Web服务器外部的人看到我正在使用的技术(例如,Apache或PHP)? - How do I block people from outside my web server from seeing what technologies I'm using (e.g., Apache or PHP)? 阻止用户直接访问此Eg.rop中的results.php(包含) - &gt;“index.php”(包括) - &gt;“results.php” - Prevent users from directly accessing results.php in this E.g. root (contains)-> “index.php” (includes)-> “results.php” 如何在错误级别为E_NOTICE的PHP中使用外部变量(例如POST / GET) - How to use external variables (e.g. POST/GET) in PHP with error level E_NOTICE 如果未知变量(例如电子邮件)在字符串中,如何在 PHP8 中检查? - How to check in PHP8 if a unknown variable (e.g. e-mail) is in a string? Symfony 2动态路由(例如用于商店) - Symfony 2 dynamic routing (e.g. for stores)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM