簡體   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 ) 等。但是,出於好奇,我還是想看看以下是如何可能的


安裝 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.

問題:我們如何在 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 腳本並生成 output。 它有效,但我想在沒有 PHP 的情況下這樣做。

  • 換句話說,Python 有類似mod_php的東西嗎?

python 有一個類似的 mod ,但它沒有被廣泛使用,而且似乎幾年沒有更新。

注意:一種常見的處理方式是使用 apache/nginx 作為 web 服務器,使用 uwsgi 作為應用程序服務器,web 服務器重定向到應用程序服務器以獲取非靜態內容 url。

由於另一個答案,我終於設法做到了:

  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 ,我只設法在全局范圍內為<VirtualHost>進行設置。 有人知道如何直接在.htaccess中進行操作嗎?

  • 如果mod_python已安裝但未啟用,則必須執行以下操作:

     a2enmod python service apache2 restart

    但這是在安裝libapache2-mod-python時自動完成的。

  • 這在 Apache VirtualHostDirectory中是必需的: AllowOverride AllRequire all granted allgranted ,以允許將處理程序直接添加.htaccess文件中。 如果沒有它,另一種方法是直接在VirtualHost定義中添加指令AddHandler...

還有一個模塊 apache 可以服務器 python 除了mod_pythonmod_wsgi你可能已經嘗試過,如果不是,可以像下面這樣完成。

如果尚未安裝,請先安裝

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM