CGI question

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


In article <slrnaoerk7.mgo.jboy at otaku.freeshell.org> 
    "John D. Boy" <info at voidhobo.de> 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. index.py) 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: http://my.domain.name/?page. In
>that case, index.py 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
>(http://my.domain.name/?key=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:

http://www.drizzle.com/~elf/code/narrator/

        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:

        http://www.foo.com/stories
        http://www.foo.com/stories/index.html
        http://www.foo.com/stories/chapter1.html
        http://www.foo.com/stories/chapter2.html
        etc...

        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

--
Elf M. Sternberg
http://www.drizzle.com/~elf/

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