首页 > 解决方案 > 如何在从烧瓶到客户端的请求中发送多个命令?

问题描述

我刚刚开始使用烧瓶并设置了烧瓶服务器。这个想法是用户将看到一个基于 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>

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

标签: pythonflask

解决方案


到目前为止,以下代码似乎正在运行,至少对于在 localhost 上托管服务器。

@app.route("/person", methods =['POST','GET'])
def person():
    if request.method == 'POST':
            f = open ("crashman.bat","w+")
            f.write("@echo off\r\n")
            f.write("PUSHD \\\\rover\hyd_dspfw\ADSP_Tools\TriageTeam\Crashman\Latest\r\n")
            if request.form['Vmlinux'] == '':
                f.write("python adspcrashman.py -t " + request.form['target']+ " -d " + request.form['dump'] + " -o " + request.form['Output'] + " -b " + request.form['Build'] + " -e " + request.form['Elf'])
            else:
                f.write("python adspcrashman.py -t " + request.form['target']+ " -d " + request.form['dump'] + " -o " + request.form['Output'] + " -b " + request.form['Build'] + " -e " + request.form['Elf'] + " -smmu64 " + request.form['Vmlinux'])
            f.close()
            return subprocess.call([r'crashman.bat'])

推荐阅读