Quantcast
Viewing latest article 49
Browse Latest Browse All 50

Python Web Hello World

This is a Python CGI Web Hello World for Windows and Linux.

  1. Install Apache
  2. Install Python
  3. Create 1.py as following.
  4. Put 1.py in htdocs directory
  5. In httpd.conf uncomment and add .py to AddHandler so you will have something like: AddHandler cgi-script .cgi .py
  6. In httpd.conf add ExecCGI to Options so you will have somthing like: Options Indexes FollowSymLinks ExecCGI
  7. Just watch out for multiple instances of Options and add to the correct one otherwise you will get different errors such as: Options ExecCGI is off in this directory.
  8. Restart  Apache
  9. Run http://localhost/1.py

#!D:/dev/Python3.1.2/python.exe
print("Content-type: text/html")
print() # this blank line is DEFINITELY needed.
print("")
print("Hello.")
print("")



Under Linux with Python 2.x the script should have 755 permission and the following is expected in /etc/apache2/sites-available/default in Ubuntu 11.04:

    ScriptAlias /cgi-bin/ /home/reza/www/cgi-bin/
    <Directory "/home/reza/www/cgi-bin">
        AllowOverride None
        Options ExecCGI SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

The script:

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""

 is expected to be under ~/www/cgi-bin/ directory and will run by: http://localhost/cgi-bin/index.py


Viewing latest article 49
Browse Latest Browse All 50

Trending Articles