How to run python scripts in centos via browser.. Python version 2.6.6 And it's working from command line but I want to run via browser.
1 Answer
Thats a great question: making the leap between a command line script and a web-site script isn't obvious. There are a few ways of converting a 'shell-script' into a web-view:
CGI scripts. This is the 'old' way of doing it. Basically you configure Apache (or some other web server) that when a url gets called, it runs your script. This way you have to produce 'cgi' stuff including headers. Its a bit of a pain to do, but works. There are some other posts which show how to do this, but you need to configure a web server as well as add in cgi bits into your code
Use a framework (like flask, or django or...) Flasks are the easiest to understand. They run as a script, and run up a mini webserver that you can poke a browser at. when run in this 'dev' mode, they're usually single threaded, but great for developing. Flasks also have a 'routing' function which allws you to have different url's go to different functions, so you can make more then one url. see http://flask.pocoo.org
Use a 'process manager' such as uwsgi. The great thing about flasks/django is that they can be ported onto uwsgi, which is a proper production process manager which will run multiple threads and do caching and other cool stuff that you need in production servers.
You could also hand-crank a tiny webserver, either using a framework like Twisted, or even by doing the socket stuff by hand. I wouldn't recommend this for any serious work (although its a great learning exercise in threads etc).
I would recommend you look at using something like Flasks or Django - I use Django the moment I have a DB to look after, otherwise Flasks for little script type things. They both have excellent tutorials.
Good luck!