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

2

u/santiagobasulto Oct 26 '14

It is possible. You could use Flask to put a simple webpage with your form. Check this out, in ten minutes you have your webapp working: http://flask.pocoo.org/docs/0.10/tutorial/

EDIT: I choosed Flask for simplicity. You can use pretty much anything. Django, Bottle, webapp2 (maybe with appengine). I was just trying to provide a simple example that it can be done easy.

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.

1

u/swims_with_spacemen Pythonista Oct 27 '14

As of 21:34 Eastern on Sunday, this thread has 5 comments - all answers are valid. 4 of them have been down voted though, with no explanation of why they were down voted. Who ever down voted them- Can you EXPLAIN why you're down voting them?

My post describes infrastructure I'm presently deploying. It works- amazingly. I even said at the top that it was probably too complex for what the user was asking, but since there was no requirements listed by the op I posted it anyway, in case he/she needed something that needed significant throughput.

/u/feuermelder suggested doing it in CGI- a valid alternative, depending on the code that's being executed.

/u/ggtsu_00 even posted CODE.

So, insightful downvoter- care to identify where the problem is here?

3

u/elbiot Oct 27 '14

My guess is someone(s) who see this as an /r/learnpython question.

0

u/[deleted] Oct 27 '14

i ask same question !! i help here http://www.reddit.com/r/Python/comments/2ka1wy/help_need_to_create_a_script_that_lists/ an onlyu get DOWN VOTES !! why ????? this reddit is HOSTILE i report to admin but no answer

1

u/swims_with_spacemen Pythonista Oct 26 '14

Judging by your question, this may be a bit more than you intend to chew off. I don't really know what exactly you're trying to accomplish, or how python you are. But:

Yes, It is possible using python. Your problem isn't having python handle the web request - there are plenty of frameworks available for that- my go-to is Tornado. Your problem will be with the latency/timeout issues with the task being RUN. If you have a long running script - such as anything to do with telnet - then your website will feel slow and poor, while a request waits for completion. If you run it in the "background" ignoring the results/output, you'll have no way to relay a failure or issue back to the user. You need something that will handle the job itself.

I'd do it like this:

[Tornado] [tornado-celery] [Celery] [celery-tasks] [RabbitMQ]

Tornado is the REST api interface. It launches Celery tasks via tornado-celery. Celery is the distributed asynchronous task runner. The Celery tasks handle the <whatever your task> and stores the result in the backend, which can then be returned with another call through tornado->celery to get the results.

So, to launch a job, you would "Post" to Tornado, and if you wanted an update on the task you've requested, you could GET status passing the id.

1

u/feuermelder Oct 26 '14

Old but very simple method would be CGI. Check it out... it's two or three more lines in your python script, if you're down to the if name == 'main' part. And don't forget to sanitize user input or you will get into trouble really fast.

1

u/noiv Oct 26 '14

Have you looked at pythonanywhere.com?

-1

u/Duskmon Oct 27 '14

If you use flask this is actually very easy. Here's a link to a flask project that does EXACTLY what you want.

Just some form requests. Good luck. If you have questions feel free to ask, I'll be around.

https://github.com/DavidAwad/basildb.me

-1

u/[deleted] Oct 28 '14

thanks man, im beginning to see the light. I have to fight a lot because I am a newbie with flask but this help me a looot.

btw, sorry everyone, maybe this should go in learnpython subreddit. sorry and thank you all! i love python community = )