Aftnix Aftnix - 3 months ago 19
Python Question

Running a shell command from a flask app

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...

Answer

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"]
              ^
Comments