-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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Saeed
  • 3,255
  • 4
  • 17
  • 36

1 Answers1

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 %}
JonSG
  • 10,542
  • 2
  • 25
  • 36
  • 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 -` – Saeed May 12 '23 at 14:18
  • ahh.. I see where your issue is now. I will update this answer – JonSG May 12 '23 at 14:20
  • 1
    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`. – Saeed May 12 '23 at 14:37
  • I made some more additions/corrections for ya :-) – JonSG May 12 '23 at 15:21
  • No, I still see white blank page in Google Chrome. – Saeed May 12 '23 at 18:23