如何在烧瓶中向客户端发送请求中的多个命令?

时间:2019-05-30 08:28:13

标签: python flask

我刚开始使用flask,并设置了flask服务器。这个想法是用户将看到一个基于Web的UI,通过它输入一些参数并点击运行按钮。然后,这些参数将传递给服务器,并且服务器应在用户的系统上打开命令提示符并执行以下操作:-

  1. 推入路径
  2. 使用用户通过UI提供的参数运行位于路径的脚本 python script.py -a arg1 -a arg2(仅作为示例)

烧瓶服务器

from flask import Flask, render_template, request
import subprocess
app = Flask(__name__)

@app.route("/")
def home():
    return render_template("home.html")

@app.route("/person", methods =['POST','GET'])
def person():
    if request.method == 'POST':
        data = request.form
        return subprocess.call([r'C:\\flask_sample\\matrix.bat'])

if __name__ == "__app__":
    app.run(debug=True)

home.html(包含应从其接收参数的表单)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <title>Crashman</title>
</head>
<body>
    <div class="container-fluid">
        <div class="jumbotron jumbotron-fluid"><h1 class="text-center display-3 text-wrap">Crashman</h1></div>
    </div>
    <div class="container-fluid">
        <form action="http://localhost:5000/person" method="POST">
        <div class="row form-group" >
            <div class="col-3"><label for="target_chipset">Target-chipset</label></div>
            <div class="col-9"><input type="text" name="target" id="target_chipset" class="form-control" placeholder="Enter target number E.g. 845"></div>
        </div>
        <div class="row form-group">
            <div class="col-3"><label for="ram_dump">Ram dump location</label></div>
            <div class="col-9"><input type="text" name="dump" id="ram_dump" class="form-control" placeholder="Enter Ramdump path (including the dump file)"></div>
        </div>
        <div class="row form-group">
            <div class="col-3"><label for="output">Output location</label></div>
            <div class="col-9"><input type="text" name="Output" id="output" class="form-control" placeholder="Enter the path where the report will be generated"></div>
        </div>
        <div class="row form-group">
            <div class="col-3"><label for="build">DSP Build location</label></div>
            <div class="col-9"><input type="text" name="Build" id="build" class="form-control" placeholder="Enter the DSP build path"></div>
        </div>
        <div class="row form-group">
            <div class="col-3"><label for="elf">Elf location</label></div>
            <div class="col-9"><input type="text" name="Elf" id="elf" class="form-control" placeholder="Enter the elf path"></div>
        </div>
        <div class="row form-group">
            <div class="col-3"><label for="vmlinux">Vmlinux location(smmu64) </label><small>Optional</small></div> 
            <div class="col-9"><input type="text" name="Vmlinux" id="vmlinux" class="form-control" placeholder="Enter the vmlinux path"></div>
        </div>
        <div class="row form-group">
            <div class="col"><button class="btn btn-success" type="submit">Run</button></div>
        </div>
    </form>
    <div class="col"><button class="btn btn-primary" onclick="create_command();">Get Command</button></div>
    </div>
    <div class="container-fluid">
            <div class="card text-center">
                    <div class="card-header">

                    </div>
                    <div class="card-body">
                      <h5 class="card-title">Your command</h5>
                      <p class="card-text" id="appended_command"></p>
                    </div>
                    <div class="card-footer text-muted">

                    </div>
            </div>
    </div>
</body>
<script type="text/javascript">
function create_command()
{
    if(document.getElementById('vmlinux').value=="")
    {
        document.getElementById('appended_command').innerHTML = "python adspcrashman.py -t " + document.getElementById('target_chipset').value + " -d " 
        + document.getElementById('ram_dump').value + " -o " + document.getElementById('output').value + " -b " 
        + document.getElementById('build').value + " -e " + document.getElementById('elf').value;
    }
    else
    {
        document.getElementById('appended_command').innerHTML = "python adspcrashman.py -t " + document.getElementById('target_chipset').value + " -d " 
        + document.getElementById('ram_dump').value + " -o " + document.getElementById('output').value + " -b " 
        + document.getElementById('build').value + " -e " + document.getElementById('elf').value + " -smmu64 "
        +document.getElementById('vmlinux').value;
    }  
}
</script>
</html>

现在,flask代码将只运行一个.bat文件(在线https://datatofish.com/batch-file-from-python/),该文件当前位于同一系统中,因此为直接路径。但是返回请求应该执行的是运行上面提到的两个命令,或者发送一个bat文件(包含两个命令)给用户以在他/她的系统中运行它。在这种情况下我该怎么办?

1 个答案:

答案 0 :(得分:0)

到目前为止,以下代码似乎可以正常工作,至少可以在本地主机上托管服务器。

assets/images