CGI: how to get referer header?
timr at probo.com
Wed Mar 26 08:26:31 CET 2003
nihilo <exnihilo at NOmyrealCAPSbox.com> wrote:
>Skip Montanaro wrote:
>> Skip> They should be available in the environment as HTTP_USER_AGENT and
>> Skip> HTTP_REFERER. Take a look at
>> >> Thanks for the help. I tried using HTTP_USER_AGENT and HTTP_REFERER,
>> >> but I get similar results, a tuple containing an empty dictionary.
>> Sorry, I should have given a specific example. Try this:
>> import os
>> print "Referer:", os.environ.get("HTTP_REFERER", "<not present>")
>> print "User Agent:", os.environ.get("HTTP_USER_AGENT", "<unknown>")
>This works great. Thanks so much for the help ;-)
But do you understand why? I think it's important to understand why one
works and the other doesn't. This particular issue, for example, is not a
Python issue in any way: it is a CGI issue.
In CGI, you do not get direct access to the HTTP headers at all.
Everything in the HTTP headers is disbursed to environment variables before
the CGI program is invoked. The content of the request is routed to the
stdin of the CGI program.
Thus, cgi.parse_header does not fetch a header and parse it. Rather, you
give it a STRING to be parsed. That string usually comes from an
environment variable, which came originally from the HTTP headers, but
cgi.parse_header doesn't actually go looking for a header.
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list