Referrer key missing form os.environ dictionary?

Νίκος nikos.gr33k at gmail.com
Wed Sep 25 11:41:37 EDT 2013


Στις 25/9/2013 6:18 μμ, ο/η Grant Edwards έγραψε:
> On 2013-09-25, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>> On Wed, 25 Sep 2013 17:04:55 +0300, ?????????? wrote:
>>
>>> I would like to check for its existence and retrieve it if possible, if
>>> its not there then default to the string "UnKnown Ref".
>>>
>>> I try to do this with:
>>>
>>> referer = os.environ.get('HTTP_REFERER', 'UnknownRef')
>>>
>>> but that doesn't return anything either.
>
> And of course that's bolloks:
>
>     Python 2.7.5 (default, Aug 29 2013, 15:13:35)
>     [GCC 4.6.3] on linux2
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> import os
>     >>> os.environ.get('HTTP_REFERER','UnkownRef')
>     'UnkownRef'
>     >>>
>
>     Python 3.2.5 (default, Aug 29 2013, 15:19:46)
>     [GCC 4.6.3] on linux2
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> import os
>     >>> os.environ.get('HTTP_REFERER','UnkownRef')
>     'UnkownRef'
>     >>>
>
>>> Can you verify that its the correct way to grab the referral string?
>
> It is.
>
>> The Referer is not an environment variable.
>
> It is when you're writing a CGI app.
>
>> How would your shell know what URL you were just browsing?
>
> Because the HTTP server sets those environment variables before
> invoking the CGI app.
>
So you mean that even if i run it via shell this stement will  also work 
because it happens to be in the same enviroment with the HTTP server?

No need to run it via web browser and check the Apache's error log?



More information about the Python-list mailing list