tech:python_flask_et_supervisor_a_la_place_des_vieille_cgi
Python, Flask et Supervisor à la place des vieilles CGI
Quand nous faisons une requête web (un POST par exemple) sur une URL, nous souhaitons lancer la commande ci-dessous
./script.sh -a -f prefix-/${POST_VAR} >> plop.log 2>&1
Voici une alternative aux CGI
Si besoin activer les dépôts backports :
echo "deb http://ftp.debian.org/debian jessie-backports main" >> /etc/apt/sources.list apt-get update
apt-get install python3-flask gunicorn3 supervisor
plop.py
#! /usr/bin/env python3 import subprocess import re from flask import Flask, request, send_from_directory, after_this_request, abort app = Flask(__name__) from subprocess import call LOGFILE='plop.log' # MUST BE ENDED BY "/" DL_DIR='/home/plop/foo/' SCRIPT_ARGS='-a -f' SCRIPT='./script.sh' def bash_command(cmd): subprocess.Popen(cmd, shell=True, executable='/bin/bash') @app.route('/') def hello_world(): return 'Hello World!' @app.route('/hello/', methods=['POST']) def hello(): var_post = request.form['var_post'] bash_command(SCRIPT + ' ' + SCRIPT_ARGS + ' ' + 'prefix-' + var_post + ' >>' + LOGFILE + ' 2>&1') return '' @app.route('/dl/<codename>') def download_file(filename): @after_this_request def delete_file(response): if re.match('^prefix-.*\.key$', filename): bash_command('shred -u ' + DL_DIR + filename) return response if re.match('^prefix-', filename): return send_from_directory(DL_DIR, filename, as_attachment=True) else: abort(403) if __name__ == '__main__': app.run( #debug=True, #host="0.0.0.0", port=int("5001") )
Pour tester sur le post TCP 5001 avant du passer en prod sur le port 8000
wsgi.py
#! /usr/bin/env python3 from plop import app if __name__ == "__main__": app.run()
/etc/supervisor/conf.d/plop.conf
[program:plop] command = gunicorn3 --bind 127.0.0.1:8000 wsgi:app directory = /home/plop/bin user = plop
Note : A la place du Supervisor elle est possible d'utiliser SystemD ou S6
tech/python_flask_et_supervisor_a_la_place_des_vieille_cgi.txt · Dernière modification : de Jean-Baptiste
