I'm trying to stream stdout of a script, myscript.py
, through a Flask endpoint.
I'm using this SO answer as my starting point. That script worked fine for me. The code here is virtually unchanged from that answer, except for calling a Python script instead of "dmesg" directly.
@app.route('/yield')
def index():
def inner():
proc = subprocess.Popen(
['python', 'myscript.py'],
shell=True,
stdout=subprocess.PIPE
)
for line in iter(proc.stdout.readline,''):
time.sleep(1)
yield line.rstrip() + '<br/>\n'
return Flask.Response(inner(), mimetype='text/html')
myscript.py
import subprocess
def main():
subprocess.Popen(['dmesg'], shell=True, stdout=subprocess.PIPE)
if __name__ == '__main__':
main()
When I navigate to "/yield" in my browser I get a "This page can't be reached" error, and the flask test server drops into a Python interpreter (see below) and nothing else happens.
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 262-399-135
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
What can I fix in the above to call a Python script for streaming through Flask?