User Tools

Site Tools


doc:appunti:linux:sa:libapache2-mod-python

mod_python per Apache

Si installa il pacchetto Debian libapache2-mod-python, quindi in un VirtualHost si abilita l'esecuzione del Python:

<Location />
    SetHandler mod_python
    PythonPath "sys.path+['/etc/apache2/']"
    #PythonHandler mod_python.testhandler
    PythonHandler mptest
    PythonDebug On
</Location>

Con SetHandler si dice di passare ogni richiesta al Python, se si desidera passare solo le richieste di file .py si può usare la direttiva AddHandler. L'handler predefinito è contenuto nel file /etc/apache2/mptest.py, il cui percorso è stato aggiunto alla sys.path con la direttiva PythonPath. Eventuali errory Python saranno mostrati nel browser grazie alla direttiva PythonDebug.

Per verificare le impostazioni si può attivare provvisoriamente l'handler mod_python.testhandler.

Questo è un esempio di handler mptest.py:

from mod_python import apache
 
def handler(req):
    req.content_type = 'text/plain'
    req.write("Hello World!")
    return apache.OK

Autenticazione con PythonAuthenHandler

Per delegare l'autenticazione Basic di Apache ad una procedura Python è possibile usare queste direttive:

<Location /pippo>
    AuthType Basic
    AuthName "Protetto con Python"
    AuthUserFile /dev/null
    AuthBasicAuthoritative Off
    PythonAuthenHandler authpippo
    Require valid-user
</Location>

L'autenticazione su file AuthUserFile viene dichiarata non autoritativa, si aggiunge un handler Python che deve essere soddisfatto. Nell'esempio verrà eseguito il file authpippo.py cercato nella sys.path. Ecco un esempio:

from mod_python import apache
 
def authenhandler(req):
    pw = req.get_basic_auth_pw()
    user = req.user
    if user == "pippo" and pw == "pippo":
        return apache.OK
    else:
        return apache.HTTP_UNAUTHORIZED
doc/appunti/linux/sa/libapache2-mod-python.txt · Last modified: 2016/03/03 22:22 by niccolo