r/Python • • Oct 26 '14

Run python scripts from web

Hello everyone,

I have some python scripts that I need people can execute via web, passing some variables to the script with a form.

Any idea? something where to start, I am so lost with this.

For example, a telnet script to enters a router and change some stuff.

What I need is a webpage with a form, where the user can pass some variables to the script, send the form, execute the script described above and show some output.

Its possible using only python? I dont want to use php in the web, I just want to use python.

any help is welcome thank you guys : )

0 Upvotes

10 comments sorted by

View all comments

2

u/ggtsu_00 Oct 26 '14

Use bottle.py a simple one-file micro web framework.

from bottle import route, run
import subprocess

@route('/run/<cmdline>')
def index(cmdline):
    p = subprocess.Popen(cmdline,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    stdout, stderr = p.communicate()
    return '<h1>stdout:</h1><pre>%s</pre><h1>stderr:</h1><pre>%s</pre>'%(stdout, stderr)

run(host='localhost', port=8080)

Make a file called script.bat that does what ever you want in the same location of your directory and it will run your python file.

Then go http://localhost:8080/run/script.bat

Btw, for safety, dont expose this on the public internet as it is a backdoor to execute arbitrary code on your PC.