[Tutor] referrer, triming [getting environmental varaibles and the 'urlparse' module]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 17 Aug 2002 21:44:53 -0700 (PDT)


On Sun, 18 Aug 2002, Kyle Babich wrote:

> Quick question, how/where do I find the HTTP_REFERRER

Hi Kyle, glad to hear from you again!

I believe that it should be one of the environmental variables available
to your CGI program.  A program's environmental variables are accessible
through the 'os.environ' dictionary.

    os.environ['HTTP_REFERRER']

should be enough to get the referrer information.


> and how would I trim it to just the file?  (Ie. if the referrer was
> http://abc.com/media/dateline.html how would I trim it to
> dateline.html?)

The 'urlparse' module has a few functions in there that can help you break
up an 'url' into pieces.

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

You'll probably need to fiddle with the urlsplit() or urlparse() functions
a bit, but it shouldn't be too bad.


I hope this helps!