test bash script execution from python

This commit is contained in:
MaelREBOUX 2021-04-06 22:25:07 +02:00
parent 19843cc6eb
commit 344365d860
2 changed files with 35 additions and 1 deletions

View file

@ -27,7 +27,7 @@ def test():
@app.route("/phase1/") @app.route("/phase1/")
def phase1(): def phase1():
stdout = check_output(['../scripts/traitements_phase_1.sh']).decode('utf-8') stdout = check_output(['/data/projets/ar_redadeg/scripts/traitements_phase_1.sh 2022']).decode('utf-8')
return stdout return stdout

34
api/test.py Normal file
View file

@ -0,0 +1,34 @@
#!usr/local/bin/python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/53380988/how-to-execute-shell-script-from-flask-app
import subprocess
from subprocess import Popen, PIPE
from subprocess import check_output
script = '/data/projets/ar_redadeg/scripts/traitements_phase_1.sh 2022'
def get_shell_script_output_using_communicate():
session = Popen([script], stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = session.communicate()
if stderr:
raise Exception("Error "+str(stderr))
return stdout.decode('utf-8')
def get_shell_script_output_using_check_output():
stdout = check_output([script]).decode('utf-8')
return stdout
def main():
return '<pre>'+get_shell_script_output_using_communicate()+'</pre>'
pass
if __name__ == "__main__":
# execute only if run as a script
main()