I have been starting to learn python lately, and mostly would like to make some web applications with it, as I hate programming in php. So I set out to figure out how to use my python scripts on my website, its pretty simple!
First make sure that your web server has python support, most new ones do so it shouldn't be a problem.
Second, load up notepad or whatever text editor you like.
The first line in your python script tells the server where python is located on the disk.
The next line sends the headers that let the browser know what type of content to expect:
And finally, we write the html we want to output
Here is the full script:
Now save your script as "hello.py" – now log in to your server with your favorite FTP program, and upload hello.py to the "cgi-bin" directory. You now need to chmod the file so that it will execute. Now you can load your file in your browser (yoursite.com/cgi-bin/hello.py)and you should see "Hello World!" output on your screen.
Now of course you could do any actual scripting you needed in that file, and output the result of that to the web – as it is, Hello World does not accomplish much.
There is also another way to use python like php using mod_python – but I have not explored that option yet as it keeps giving me errors when I try to install it on my server, but for now the cgi method should work well.