【问题标题】:Running shell script on flask在烧瓶上运行 shell 脚本
【发布时间】:2015-06-04 07:20:50
【问题描述】:

我的应用程序已经设置了以下 app.py 文件和两个 .html 文件:index.html(基本模板)和 upload.html,客户端可以在其中看到他刚刚上传的图像。我遇到的问题是,我希望我的程序(可能是 app.py)在用户被重定向到 upload.html 模板之前执行一个 matlab 函数。我找到了关于如何在烧瓶上运行 bash shell 命令的问答(但这不是命令),但我还没有找到脚本。

我得到的解决方法是创建一个 shell 脚本:hack.sh,它将运行 matlab 代码。在我的终端中,这很简单:

$bash hack.sh

hack.sh:

nohup matlab -nodisplay -nosplash -r run_image_alg > text_output.txt &

run_image_alg 是我的 matlab 文件 (run_image_alg.m)

这是我的 app.py 代码:

import os

from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename

# Initialize the Flask application

app = Flask(__name__)

# This will be th path to the upload directory
app.config['UPLOAD_FOLDER'] = 'uploads/'

# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['png','jpg','jpeg'])

# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
  return '.' in filename and \
    filename.rsplit('.',1)[1] in app.config['ALLOWED_EXTENSIONS']

# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the 
# value of the operation

@app.route('/')
def index():
  return render_template('index.html')

#Route that will process the file upload
@app.route('/upload',methods=['POST'])
def upload():
  uploaded_files = request.files.getlist("file[]")
  filenames = []
  for file in uploaded_files:
    if file and allowed_file(file.filename):
      filename = secure_filename(file.filename)
      file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
      filenames.append(filename)

  print uploaded_files

  #RUN BASH SCRIPT HERE.


  return render_template('upload.html',filenames=filenames)

@app.route('/uploads/<filename>')
def uploaded_file(filename):
  return send_from_directory(app.config['UPLOAD_FOLDER'],filename)


if __name__ == '__main__':
  app.run(
    host='0.0.0.0',
    #port=int("80"),
    debug=True
  )

我可能想念一个图书馆?我在 stackoverflow 上发现了一个类似的问答,有人想运行(已知的)shell 命令($ls -l)。我的情况不同,因为它不是已知命令,而是创建的脚本:

from flask import Flask
import subprocess

app = Flask(__name__)

@app.route("/")

def hello():
    cmd = ["ls","-l"]
    p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            stdin=subprocess.PIPE)
    out,err = p.communicate()
    return out
if __name__ == "__main__" :
    app.run()

【问题讨论】:

    标签: python shell flask


    【解决方案1】:

    如果你想运行 matlab,只需将你的命令更改为

    cmd = ["matlab", "-nodisplay", "-nosplash", "-r", "run_image_alg"]
    

    如果要将输出重定向到文件:

    with open('text_output.txt', 'w') as fout:
        subprocess.Popen(cmd, stdout=fout,
                              stderr=subprocess.PIPE,
                              stdin=subprocess.PIPE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多