I am trying to run a c executable from a flask application. I created two input for the arguments and one button to submit and run the executable.
What I want to execute is this command line: ./Ex02_DriveStatus 5 blablabla.sw
The executable Ex02_DriveStatus
is on the same folder than the python file.
When I run it from the terminal ./Ex02_DriveStatus 5 blablabla.sw
it's working well.But I would like to be able to run it from my flask application/ web interface.
Here is my python code:
import canopen
from datetime import datetime
import time
import os
import numpy as np
import argparse
from DriveLib import Drive
from flask import Flask
from flask import request
from flask import render_template
import subprocess
from subprocess import PIPE
# os.system("sudo ifconfig can0 down")
# os.system("sudo ip link set can0 type can bitrate 500000")
# os.system("sudo ifconfig can0 up")
app = Flask(__name__)
@app.route('/')
def home():
return render_template('flashing.html')
@app.route('/flashing/')
def home_flash():
return render_template('flashing.html')
@app.route('/flashing/', methods=['POST'])
def flash():
global node_id
global file_name
node_id = request.form['node_id']
file_name = request.form['file_name']
if request.form.get("submit"):
node_id = request.form['node_id']
file_name = request.form['file_name']
if node_id == '':
node_id = 0
if file_name == '':
file_name = 0
if request.method == 'POST':
output = run(node_id, file_name)
return render_template('flashing.html', output=output, node_id=node_id, file_name=file_name)
def run(node_id, file_name):
s=subprocess.run(["./Ex02_DriveStatus", node_id, file_name],stdout=PIPE)
print(s)
return s.stdout.decode("utf-8")
And my HTML code:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href='/static/main.css'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type=text/javascript src="{{
url_for('static', filename='main.js') }}"></script>
<script>
window.addEventListener("load", function(){
// BASIC
numpad.attach({target: "demoA"});
// WITH OPTIONS
numpad.attach({target: "demoB"});
numpad.attach({target: "demoC"});
});
</script>
<title>FLASH the drive</title>
<p class="title"><strong>FLASH THE DRIVE</strong></p>
</head>
<body>
<div id="menu">
<ul id="onglets" >
<li class="active"><a href="/flashing/"> Flash drive </a></li>
<li><a href="/test_drive/"> Test drive </a></li>
<li><a href="/test_module/"> Test module </a></li>
</ul>
</div>
<div class="content">
<!--test form-->
<form method="post" action="/flashing/">
<textarea id="output" name="output" rows="30" cols="50" style="resize:none" placeholder="//Your output here.">{{output}}</textarea><br>
<table>
<tr>
<td> <p>Chose the ID you want to flash with:</p> </td>
<td> <input name="node_id" id="demoA" style="height:50px;"> </td>
</tr>
<tr>
<td> <p>File name:</p> </td>
<td> <input name="file_name" style="height:50px;"> </td>
</tr>
<tr>
<td> <input id="buttonid" type="submit" name="submit" class="submit" value="FLASH"> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
When I try this flask run
it's running for ever without results.
I tried to create a python file test.py
with just a print print("hello")
.
I assume this line is the problem:
s=subprocess.run(["./Ex02_DriveStatus", node_id, file_name],stdout=PIPE)
so I tested it with a simple file (the test.py):
s=subprocess.run(["python", file_name],stdout=PIPE)
and on the file_name
input I would enter test.py
and it is working.
So I don't know if it is because the executable from c gives me some conflict or if there is something I didn't write well.
CodePudding user response:
Make sure your program is outputting to STDOUT.
Try removing the stdout=PIPE
or try piping STDERR as well.