[Tutor] cgi question

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Fri, 18 Aug 2000 22:04:17 -0700 (PDT)

> Instead of displaying the results from "gush" or "whimper" -- the
> routines which process the cgi input, the whole python script appears in
> the browser -- just exactly as it appears in the Python IDE.  If this is
> clue enough for you, stop reading.  Otherwise, read on.

Stopped.  *grin*

Ok, the answer depends on what server you're running on;  What's happening
is that your web server is treating those files as plain text, and not as
executable content.  You'll need to do some additional setup to associate
.py files as CGI scripts.  If you're using IIS, take a look at section 8.1
in the Python FAQ for details:


If it's Apache, you'll want to first tell Python that ".py" is a perfectly
good extension for a cgi script.  Look at httpd.conf for a line that says
"AddHandler cgi-script", and add another extension to the list, like this:

# To use CGI scripts:
AddHandler cgi-script .cgi .py

After this, you might still need to do a few more changes to Apache, since
CGI privleges are usually only allowed in certain directories, for
safety's sake.  For me, this is /home/httpd/cgi-bin, but you can add more
permissible settings, I think, using the ScriptAlias configuration line or
the ExecCGI option.

It might be best, though, just to make sure that your scripts work in
cgi-bin first.  Place them there, and see if they work.  You can always
relax your security restrictions afterwards, when you feel more

Finally, if you're running on a UNIX system, you'll need to add in the
front of your script something like:

#!/usr/bin/env python

at the front of the file, before anything else.  That line tells the
system how to execute your Python program.  The reason for this is because
any language can be used to run a CGI script.  Analogously, the Perl
people usually put

#!/usr/bin/perl -w