First, you need an Apache server on your Linux/Mac OS/Windows box. If you are gonna use data base, you need a database server. There are tons of blogs addressing these issues. So I won't be gossipy here. Suppose everything we mention below happens on your server-even web browsing.
Second, configure cgi in Apache. There are many ways to run a Python program on a web/http interface. I think CGI is the easiest. Assume you are on your own server and using all default settings. On Ubuntu Linux 9.04, the default configuration file for your default website is
/etc/apache2/sites-available/default
Open it, find the part for cgi directory, and make it like this ScriptAlias /cgi-bin/ /var/www/cgi-bin/The lineAllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all AddHandler cgi-script .py # tell Apache to handle every file with .py suffix as a cgi program AddHandler default-handler .html .htm # tell Apache to handle HTML files in regular way
ScriptAlias /cgi-bin/ /var/www/cgi-bin/specifies the path of cgi codes and the actual directory of your program. So when you type http://127.0.0.1/cgi-bin, the Apache will look into the directory
/var/www/cgi-bin/
of your localhost.Now restart your apache. On Ubuntu Linux, by default installation and configuration, it is
sudo /etc/init.d/apache2 restartThird, write up a Hello, World! program. Save it as hello.py and give it execution privilege for anyone.
#!/usr/bin/env python print "Content-Type: text/html" print print """\Now open http://127.0.0.1/cgi-bin/hello.py in your web browser and you shall see a hello world in it.First Python HTTP Programming Hello World!
"""
If you have any error, e.g., 500 error, please check your Apache log file. It shall tell you something. You can find errors like what you have seen from a standard Python interpreter.
Reference: http://webpython.codepoint.net/cgi_tutorial
No comments:
Post a Comment