Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a shell command from a flask app [closed]

I'm trying to run a shell command from a flask app and trying to grab the output. The app I'm trying with is following:

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()

The shell command is ok. I checked it outside, but from the browser I'm getting "internal sever error".

EDIT: As the first answer pointed out it had a typo...But now its running ok but I'm not getting any output in my browser...

like image 421
Aftnix Avatar asked Jun 01 '14 06:06

Aftnix


People also ask

How do I run a flask in the background?

Make sure, you close the terminal and not press Ctrl + C. This will allow it to run in background even when you log out. To stop it from running , ssh in to the pi again and run ps -ef |grep nohup and kill -9 XXXXX where XXXX is the pid you will get ps command.

How do you exit a flask run?

Goto Task Manager. Find flask.exe. Select and End process.

How do you start a flask shell?

Starting with Flask 0.11 the recommended way to work with the shell is the flask shell command which does a lot of this automatically for you. For instance the shell is automatically initialized with a loaded application context. For more information see Command Line Interface.


1 Answers

It's a simple typo. cd in the following line should be cmd:

p = subprocess.Popen(cd, # <----
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE,
                     stdin=subprocess.PIPE)

UPDATE

There's another typo; remove a space in the second item:

cmd = ["ls", " -l"]
              ^
like image 143
falsetru Avatar answered Oct 12 '22 23:10

falsetru