Referrer key missing form os.environ dictionary?
Tim Chase
python.list at tim.thechases.com
Wed Sep 25 11:14:50 EDT 2013
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()
-tkc
More information about the Python-list
mailing list