[Tutor] Automated Breadcrumb -- help for a PHP user

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Nov 25 16:56:15 EST 2003



On Tue, 25 Nov 2003, Chris Heisel wrote:

> I'd like to do this in Python and make it a cgi-bin file so I could have
> something like this:
> <!--#include virtual="/cgi-bin/bread.py" -->


Hi Chris,

This looks like a Server Side Include (SSI) directive.

    http://httpd.apache.org/docs-2.0/howto/ssi.html
    http://httpd.apache.org/docs-2.0/mod/mod_include.html


According to the second document, it is possible to pass parameters to
bread.py: we can add a query string to the end.  For example:

    <!--#include virtual="/cgi-bin/bread.py?path=/foo/bar" -->

will call bread.py, and pass 'path' as a CGI parameter.


But according to that second page, you can also grab at the 'URI' path by
looking at the environment variable 'DOCUMENT_URI'.  I'm guessing that
DOCUMENT_URI is equivalent to $PHP_SELF.  If so, then we can take full
advanage of that!


Here's a small cgi that just prints out the document uri:

###
#!/usr/local/bin/python2.3

import cgitb
cgitb.enable()
import os

print "Content-type: text/plain\n\n"
print os.environ['DOCUMENT_URI']
###

If you just include this cgi by using the SSI directive, you should see
that the cgi program now knows about the document uri.


> One its there I'm sure I can muddle through some flow control structures
> and what not to massage the URL into the various breadcrumb pieces...

Sounds good!  You may find:

    http://www.python.org/doc/lib/module-urlparse.html

useful; it will already handle what you did with PHP's explode() function.
But if you really want a close equivalent to explode(), you can use a
string's split() method:

###
>>> 'hello-world-this-is-a-test'.split('-')
['hello', 'world', 'this', 'is', 'a', 'test']
###


Good luck to you!





More information about the Tutor mailing list