【问题标题】:How to let the webserver (e.g. Apache) call Python directly?如何让网络服务器(例如 Apache)直接调用 Python?
【发布时间】:2020-02-13 06:43:15
【问题描述】:

(重要)免责声明:我知道这可能不是一个好主意,Python 不像 PHP,并且使用 Python 做 Web 的“自然”方式更多是通过使用像 Bottle、Flask、 Django(我已经使用过)等。但是,出于好奇,我还是想看看以下是如何实现的


安装Apache + PHP后,我们可以访问http://www.example.com/index.php这样的页面。在内部,Apache 可能将请求传递给 PHP,PHP 执行代码,生成文本输出,然后由 Apache 提供服务。

问题:我们如何在 Python 中做类似的事情?即通过访问 http://www.example.com/index.py,Apache 将调用脚本 index.py

print("<html><body>Hello world</body></html>")

然后 Apache 会将此页面提供给客户端。


注意:

  • 调用http://www.example.com/index.py?foo=bar 甚至可以将参数提供给sys.argv 中的Python 脚本

  • 我已经这样做了:http://www.example.com/index.php:

    <?php 
    $out = shell_exec("python index.py"); 
    echo($out); 
    ?>
    

    然后调用 Python 脚本并生成输出。它可以工作,但我想不使用 PHP。

  • 换一种说法,Python 有没有类似mod_php 的东西?

【问题讨论】:

  • apache 有模块 mod_python 来运行 python 脚本,还有 mod_fcgi 来运行任何语言的脚本(即 python、perl、ruby、bash)。
  • 查看Python - CGI programming 了解如何为mod_fcgi 编写代码
  • 关于赏金的注释(我忘了添加注释):mod_python 经常被引用为不再维护。现在有没有一个很好的解决方案,不需要需要 Python 框架,并且不需要在端口上侦听不断运行的脚本。相反,我希望在调用 www.example.com/test.py 时,网络服务器(apache 或 nginx)直接调用 test.py。
  • 总是必须是一些始终运行并侦听端口的代码。如果你运行 Apache/Nginx/WSCGI,那么这个服务器会一直运行并监听端口。而且我不会期望不需要 Python 框架的新解决方案,因为框架更容易,而且代码组织得更好。即使在 PHP 中,也有很多人更喜欢 PHP 框架而不是纯 PHP。

标签: php python apache


【解决方案1】:

有一个用于 python 的similar mod,但它没有被广泛使用,而且似乎已经有几年没有更新了。

注意:一种常见的处理方式是使用 apache/nginx 作为 web 服务器,使用 uwsgi 作为应用程序服务器,web 服务器重定向到应用程序服务器以获得非静态内容 url。

【讨论】:

  • 谢谢你,我听从了你的建议,它有效(见我的另一个答案)!
  • 我不知道为什么,但不可能在.htaccess 中设置AddHandler mod_python .py,我只能在全局范围内为&lt;VirtualHost&gt; 设置。您是否知道如何直接在.htaccess 中进行操作?
  • @Basj:不,抱歉,几年前我只是玩弄了一下,不知道任何细节。
  • 能够直接在.htaccess @blue_note中实现的解决方案是:&lt;VirtualHost&gt; &lt;Directory /&gt; AllowOverride All...
【解决方案2】:

多亏了另一个答案,我终于成功了:

  1. 做:

    apt-get install libapache2-mod-python
    
  2. 然后在您的网站文件夹中创建或打开.htaccess 文件,并添加

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

    def index(req):
        return("<html><body>Hello world</body></html>")
    
  4. 现在访问www.example.com/test.py 有效!


注意:

  • def index(req) 确实是必需的:使用另一个名称会失败。

  • 我不知道为什么,但不可能在.htaccess 中设置AddHandler mod_python .py,我只能在全局范围内为&lt;VirtualHost&gt; 设置。有人知道如何直接在.htaccess 中进行操作吗?

  • 如果mod_python已经安装但未启用,你必须这样做:

    a2enmod python               
    service apache2 restart    
    

    但这是在安装libapache2-mod-python时自动完成的。

  • 这在 Apache VirtualHostDirectory:AllowOverride AllRequire all granted 中是必需的,以允许将处理程序直接添加到 .htaccess 文件中。如果没有它,另一种方法是直接在VirtualHost 定义中添加指令AddHandler ...

【讨论】:

  • 注意:这可行,但mod_python 经常被引用为未维护。是否有最新的解决方案(注意:不需要任何框架或持续运行的脚本)。
【解决方案3】:

除了mod_python mod_wsgi 之外,还有一个模块 apache 可以为 python 提供服务,如果不是,您可能已经尝试过了,可以像下面这样完成。

如果尚未安装,请先安装

sudo apt-get install libapache2-mod-wsgi -y

创建虚拟主机

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

创建 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]

【讨论】:

  • 有没有办法让 mod_wsgi 像 .py 处理程序一样工作?即不手动指定WSGIScriptAlias /myapp /var/www/html/wsgi/index.pywww.example.com/test.py会自动调用/var/www/html/wsgi/test.pywww.example.com/index.py会自动调用/var/www/html/wsgi/index.py等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多