Referrer key missing form os.environ dictionary?

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


Στις 25/9/2013 6:14 μμ, ο/η Tim Chase έγραψε:
> On 2013-09-25 18:02, Νίκος wrote:
>> This indeed works now:
>>
>> ref = os.environ.get('HTTP_REFERER', 'Άγνωστο Ref')
>>
>> but iam wondering why this doesnt work also:
>>
>> ref = os.environ('HTTP_REFERER')
>>
>> Shouldnt both work?
>
> No...that calls os.environ.  You likely *mean*
>
>    ref = os.environ['HTTP_REFERER']
>
> using square brackets.  However, as previously noted by multiple
> respondents, this header is not guaranteed to be sent by the browser
> for a variety of reasons, so it may not be in the environment
> dictionary at all. Thus you are likely to get KeyError exceptions if
> you don't use .get() unless you wrap it in a check:
>
>    if "HTTP_REFERER" in os.environ:
>      ref = os.environ["HTTP_REFERER"]
>    else:
>      deal_with_this_situation()

Thank you very much for the clarification.
I just wanted to know the differecne between the 2 ways of using the 
HTTP_REFERER.



More information about the Python-list mailing list