Monday, May 26, 2008

CGI Python scripts on Apache for Windows

Just a quick tip how to make a CGI script running on Windows under Apache. The main problem with CGI scripts is that their first line usually contains a path to executable program - Python interpreter - and if on *nix platform this line is more or less the same:

#!/usr/bin/env python

in windows it usually different from one installation to another. If in *nix environment you do not have to change this line most of the time, in windows most of the time you'll need. Luckily, Apache developers invented an option to lookup the path for script interpreter from the registry by following association of file extension. Look at this .htaccess for example:

Options +ExecCGI
AddHandler cgi-script .py

<FilesMatch "\.py$">
# Use the interpreter found in registry by file association
ScriptInterpreterSource Registry

</FilesMatch>

Here ScriptInterpreterSource Registry is the magic phrase to turn on the lookup. It is well described in Apache manual. FilesMatch is another directive to limit the scope of lookup to .py files only.

Finally, a reminder of how a minimal CGI script in Python looks like.

#!/usr/bin/env python

print "Content-Type: text/html" # HTML is following
print # blank line, end of headers

print "xxx"

No comments:

Post a Comment