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

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.