1

I have a command line tool like as follow:

tool -o output -a authentication.txt  -i input.txt

(it gets some files as input)

what I would like to do is to provide an web interface for this command line tool using flask or django since the tool is based on python. So can you guide me how can I call the tool and gets its results from stdout! Also tool has the config file and I want to open and edit it in web interface too.

My initial idea was to call python subprocess to call the app to gets its stdout but do not know how wise it is?s

davidism
  • 121,510
  • 29
  • 395
  • 339
user2091416
  • 77
  • 2
  • 10
  • 1
    There's nothing wrong with using `subprocess`. Just keep in mind it will block the request until the process finishes. – Jessie Mar 13 '17 at 21:53

2 Answers2

1

There are many ways to do it! I have no idea what you are trying to do. But basic example would be something like this. You mention that it is python program/app. You can import you command line tool as a package. If it is not package, then package it https://packaging.python.org/. Parameters you pass as arguments, you can pass in your app. In Flask you would do something like this. pseudo app.py

import tool
@app.route("/")
def index():
    return render_template("index.html")

@app.route('/tool')
def tool(input):
auth = tool.auth()
input = tool.input(input)

return output
oshaiken
  • 2,593
  • 1
  • 15
  • 25
  • thanks and if I want to let user to choose parameters? since one of them could be optional and there are another parameters too – user2091416 Mar 13 '17 at 22:08
  • http://stackoverflow.com/questions/24892035/python-flask-how-to-get-parameters-from-a-url – oshaiken Mar 13 '17 at 22:24
0

Assuming Python 3.5:

import subprocess
res = subprocess.run(["tool", "-o", "output", "-a", "authentication.txt", "-i", "input.txt"])
res.stdout

In an earlier version, you could potentially use the following, which would raise a CalledProcessError on non-zero return codes.

print(subprocess.check_output(
    "/usr/local/bin/spam bacon spam",
    stderr=subprocess.STDOUT,
    shell=True))

 # => EGGS AND SPAM!

Do be careful with running commands using untrustworthy (e.g. user) input, make sure that arguments are properly sanitized. Remember to test for malicious input, such as attempting to escape the command to run something different.

Try to ensure that the system user (typically the application user / web server user in this case) that winds up executing the command can't muck up too much if/when something inevitably makes it through by restricting its rights on the machine.