Direct to the how-to 🙂
- First make sure your hosting supports Python. Now a days almost all shared hosting providers support Python.
- Find your python installation location, this is required for “shebang” line added at the top of your python script. Usually this is /usr/bin/python or /usr/local/bin/python
- Make sure to include two print statements which will print the content-type and an empty line.
- Now put the script in cgi-bin directory inside your root public folder, usually public_html
- Change the permission of the script to 0755
- Try to browse now 🙂
print ‘Content-type: text/html’
print ‘Hello, World!’
Now look at the top of hello.py script, you will notice the ‘shebang’ is #! /usr/bin/python.And also the first two print statements 🙂
Now its little bit annoying to put all python scripts inside that cgi-bin directory. So heres what you will need to do to run a python script inside any directory…
- Make an empty .htaccss file inside the directory you want to drop your python scripts. (Just open the existing .htaccess file, make sure you have enabled ‘Show Hidden Files’ in cPanel File Manager or in your favorite FTP manager)
- Add the following two lines and save.
AddHandler cgi-script .py
- You will get 500 error if anything goes wrong.
- Make sure to clear your cache, when you modify any settings/content
- And there is always the nice people waiting for you in hosting support section, try them 🙂
I hope it helps. Let me know your comment 🙂