bash 如何从 Flask 应用程序执行 Shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53380988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:03:13  来源:igfitidea点击:

How to execute Shell Script from Flask App

pythonbashshellflask

提问by jKraut

I've created and deployed a Flask App with Apache2 server WSGI in which now would like to run a .sh script from the App. However, from calling from python code, it doesn't execute.

我已经创建并部署了一个带有 Apache2 服务器 WSGI 的 Flask 应用程序,现在想要从应用程序运行 .sh 脚本。但是,从 python 代码调用,它不会执行。

Here is the test.sh:

这是 test.sh:

#!/bin/bash
echo "hi from shell script"

Here is my python flask app code index.py (runs when App is opened) but nothing is printed or executed:

这是我的 python flask 应用程序代码 index.py(在打开应用程序时运行)但没有打印或执行任何内容:

import subprocess
subprocess.call('/var/www/FlaskApp/FlaskApp/scripts/test.sh')

To check that there is not errors in my code, I've check flask error logs, and no errors. Also, I created a script called test_shell_script.py with same python code as above (but not flask app code) and it runs great like this:

为了检查我的代码中没有错误,我检查了烧瓶错误日志,没有错误。此外,我创建了一个名为 test_shell_script.py 的脚本,其 Python 代码与上述相同(但不是 Flask 应用程序代码),它运行良好,如下所示:

# test_shell_script.py
import subprocess
subprocess.call('/var/www/FlaskApp/FlaskApp/scripts/test.sh')

And then run it with python: python3 /var/www/FlaskApp/FlaskApp/test_shell_script.py

然后用 python 运行它: python3 /var/www/FlaskApp/FlaskApp/test_shell_script.py

hi from shell script

I did change the permissions as well:

我也确实更改了权限:

-rwxr-xr-x 1 root root 364 Nov 19 17:48 ../scripts/test.sh

What am I missing here which is not allowing my Flask app to run shell commands from the python code?

我在这里缺少什么不允许我的 Flask 应用程序从 python 代码运行 shell 命令?

回答by arsho

To show command output inside Python, there are two popular methods:

要在 Python 中显示命令输出,有两种流行的方法:

  1. check_output(): It runs command with arguments and return its output. (official documentation)
  2. subprocess.communicate(): Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. (official documentation)
  1. check_output():它运行带参数的命令并返回其输出。(官方文档)
  2. subprocess.communicate(): 与进程交互:将数据发送到标准输入。从 stdout 和 stderr 读取数据,直到到达文件尾。(官方文档)

I could view the shell file output using both methods using Python 3.5in an Ubuntumachine.

我可以在Ubuntu机器中使用Python 3.5使用这两种方法查看 shell 文件输出。

app.py:

app.py

import subprocess
from subprocess import Popen, PIPE
from subprocess import check_output
from flask import Flask

def get_shell_script_output_using_communicate():
    session = subprocess.Popen(['./some.sh'], stdout=PIPE, stderr=PIPE)
    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(['./some.sh']).decode('utf-8')
    return stdout

app = Flask(__name__)

@app.route('/',methods=['GET',])
def home():
    return '<pre>'+get_shell_script_output_using_check_output()+'</pre>'

app.run(debug=True)

some.sh:

some.sh

#!/bin/bash
echo "hi from shell script"
echo "hello from shell script"

Output screenshot:

输出截图:

enter image description here

在此处输入图片说明