-1

I started learning Flask recently and I'm at the early stages.

I googled and these are the links I used to help:

this and this.

I'm trying to run a simple command like ls -lh /home in Flask when I call http://localhost/ls and show me this output in the web browser:

root@saeed:~# ls -lh /home
total 0
drwxr-xr-x  5 root  root   70 May  8 23:47 git/
drwxr-xr-x  2 root  root   97 May  9 12:07 images/
drwxr-xr-x  3 root  root   20 May 10 04:16 node/
drwxr-xr-x  3 root  root  101 May 11 01:25 python/
drwxr-x---  4 saeed saeed 137 Jan  7 10:48 saeed/

I mean when I enter http://localhost, I see the above output in my internet browser.

I tried different things (you may see the history of this question), but none of them worked and I see a blank white screen in the web browser.

How to do this?

app.py:

from flask import Flask, render_template
import subprocess
app = Flask(__name__)

@app.route('/')
def index():
  # something here to run a simple `pwd` or `ls -l`

templates/index.html:

{% extends 'base.html' %}
# something here to show the output of `pwd` or `ls -l` in the web
2
  • @JonSG no, I'm still seeing the output in the terminal, not the browser. Commented May 12, 2023 at 14:01
  • You have to tell subprocess.call to collect stdout. By default it just inherits stdout from the parent process. Commented May 12, 2023 at 15:35

1 Answer 1

0

This seems to work for me:

import subprocess
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
cites = result.stdout.decode()

Let me know if you try it so I can delete this as if it does work then we should go with the duplicate.

Once you have the cities output from the sub process, your next issue is returning that to your template. At the moment you do:

def ls():
    if True:
        cites = subprocess.call(['ls -lh /home'], shell=True)
        cites
        # print('', 200)  # no content
        return '', 200  # no content
    else:
        return '', 200  # no content
    return render_template('ls.html', cites=cites)

However, that always follows the True path and that always returns "". You want something more like:

def ls():
    cites = subprocess.run(['ls -lh /home'], stdout=subprocess.PIPE)
    if cites:
        return render_template('ls.html', cites=cites.split("\n"))
    return ('', 204)

Then you probably want to do:

{% block content %}
    <h1>{% block title %} contents {% endblock %}</h1>
    {% for cite in cites %}
        <div class='cites'>
            <h3>{{ cite }}</p>
        </div>
    {% endfor %}
{% endblock %}
Sign up to request clarification or add additional context in comments.

5 Comments

What's your ls.html contents? I see this log in the terminal but still no html output: 127.0.0.1 - - [12/May/2023 14:17:48] "GET /ls HTTP/1.1" 200 -
ahh.. I see where your issue is now. I will update this answer
Thanks, I now see what I have in ls.html, but I don't see the output of ls -lh in the web. Also in if cities, you've misspelled cites.
I made some more additions/corrections for ya :-)
No, I still see white blank page in Google Chrome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.