Shammer's Philosophy

My private adversaria

Apache python inline CGI

Python sample code is here. This source code file extension should be .py and located in /usr/lib/cgi-bin/ directory and attached exec permission by chmod 755 ***.py(*** means file name).

import cgi

print "Content-Type: text/html"     # This is important!
print  

print "<html><head><title>Python CGI Test</title></head><body>Python CGI Debut!</body></html>"

The code "cgi.test()" is good too. This script display about the environment informations.

The following code can get form data.

# The option keep_blank_values=true means that keep blank post parameter as blank value.
# If this option is not set, blank post parameter access would be failed with no such post parameter message.
form = cgi.FieldStorage(keep_blank_values=true) 
form.getvalue('post-param', '')

And Apache requires changing configuration, such as httpd.conf, apache2.conf.
At first, adding this line to common configuration file(Debian is separated common config file and each site config file).

AddHandler cgi-script .py

Check the script alias in site config. Above example is assumed below config. +ExecGCI is important, maybe. In my debian environment, this option is enabled as default. I have never tested without this option.

ScriptAlias /cgi-bin/ "/usr/lib/cgi-bin/"

<Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
</Directory>
>||