Outils pour utilisateurs

Outils du site


tech:python_flask_gunicorn_-_alternative_a_cgi

Python Flask Gunicorn - Alternative à CGI

Voir :

Alternative à Flask :

  • CherryPy
  • Falcon

Alternative à Unicorn

  • Uvicorn
apt-get install python3-flask gunicorn3

wsgi.py

#! /usr/bin/env python3
 
from plopweb import app                 
 
if __name__ == "__main__":             
    app.run(                                                                                          
            host="0.0.0.0",
            port=int("5001")
            )

plopweb.py

#! /usr/bin/env python3    
 
import subprocess                                                  
from flask import Flask, request, send_from_directory, after_this_request, abort
app = Flask(__name__)
from subprocess import call
 
LOGFILE='/tmp/plop.txt'
CMD_ANSIBLE='/usr/bin/ansible-playbook'
 
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():
    playbook = request.form['playbook']
    ip = request.environ['REMOTE_ADDR']
    bash_command('echo ' + CMD_ANSIBLE + ' ' + playbook +' -i ' + ip + ',' + '>>' + LOGFILE + ' 2>&1')
    return ''
 
if __name__ == '__main__':
    app.run(
            debug=True,
            host="0.0.0.0",
            port=int("5001")
            )

wsgi.py

#! /usr/bin/env python3
 
from plopweb import app
 
if __name__ == "__main__":
    app.run(
            host="0.0.0.0",
            port=int("5001")
            )
gunicorn3 --bind 0.0.0.0:5001  wsgi:app

Avec ça vous pouvez faire un script SystemD ou utiliser Supervisor, et mettre un nginx en reverse proxy

Exemple supervisor

apt-get install supervisor

supervisor-plopweb.conf

[program:plopweb]
command = gunicorn3 --bind 127.0.0.1:8000  wsgi:app
directory = /home/pkiweb/bin
user = plopweb

Exemple de conf nginx

plopweb

server {
        listen 443 ssl;
        server_name plopweb.lan ;
 
        access_log /var/log/nginx/plopweb-ssl-access.log;
        error_log  /var/log/nginx/plopweb-ssl-error.log;
 
        ssl_certificate     /etc/nginx/ssl/plopweb.lan.combined.crt;
        ssl_certificate_key /etc/nginx/ssl/plopweb.lan.key;
        ssl_client_certificate /etc/nginx/ssl/ca.crt;
        ssl_verify_client on;
 
        location / {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
        }
}
tech/python_flask_gunicorn_-_alternative_a_cgi.txt · Dernière modification : de Jean-Baptiste

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki