CGI question

Elf Sternberg elf at
Wed Sep 18 13:49:39 EDT 2002

In article <slrnaoerk7.mgo.jboy at> 
    "John D. Boy" <info at> writes:

>I am working on a simple CGI script to use as a framework for a website
>of mine. Basically, I want it to work as follows: When somebody accesses
>the site, a CGI (called e.g. is run. It takes an html-template
>and fills it with content from another file, called e.g. index.dat, and
>returns the completed html-document. When I want to present another
>page, I want the url to look like this: In
>that case, would fill the same template with the content of a
>file called e.g. page.dat. 
>Now, my problem is to find out what follows behind the question mark in
>my CGI script. Unless the key has a value
>(, it is not in the cgi.FieldStorage
>dictionary. Is it simply impossible, or am I not using the right
>function calls? 

        I wrote something similar recently.  You can find it at:

        It's a bit different from what you want.  I wanted to write a
CGI script that would manage a "book", meaning front matter, table of
contents, and chapters; I wanted it to be able to distinguish between
browsers that use style sheets and those that do not, and supply
different HTML to meet different needs.  And I wanted to obscure the
fact that it's a CGI.  URLs needed to look like:

        The trick is that the element "stories" in the above URL is the
CGI script: everything after that is passed to the program by the
environment variable "PATH_INFO".  By using this line:

         pathinfo = os.environ.get( 'PATH_INFO', '' )

        ... and then using a lookup table, I can identify what the user
asked for and respond to it.  I can then supply the four necessary
pieces (splash page, index page, chapters page, and 404 page).  The big
advantage to this is that robots, spiders and other indexing engines
will troll the story archives and index them because they _look like_
they're plain ol' HTML.  The other advantage is that the story entries
themselves can be compressed using GZIP, saving me on disk space, which
my ISP charges for.

        If you're using Apache, the settings needed to get this working
are trivial.  In your .htaccess file, put:

DefaultType application/x-httpd-cgi
XBitHack on
ErrorDocument 404 /~elf/error.html

        Apache will attempt to run everything that does not have an
extension and has the executable bit set.  

        Hope this helps.


Elf M. Sternberg

No bastard ever won a war by dying for his country.  He won it by making
the other poor dumb bastard die for his country.  - Gen. George S. Patton.

More information about the Python-list mailing list