Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'subject:form': 0.07; 'subject:missing': 0.07; 'raises': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'clause.': 0.16; 'subject:key': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'cc:addr:python.org': 0.22; 'header:User- Agent:1': 0.23; 'environment': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'keyerror:': 0.31; 'could': 0.34; 'except': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'skip:o 30': 0.61; 'john': 0.61; 'missing.': 0.84; 'to:addr:gordon': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=vSzvyLk9zbZYehYiKJ4mjcRtPMyyGspLyEqM58kL/aA=; b=UH2KRGxW3W/Tzkjcbu14+NsLs1fsqH7HInuKJvD8EjvkIOt1SD3Qr4iFoNtLZvF82p oSliA3x7tXA7DbTGdEzAXmeCFgjP/ZgLUIn8WTacSVvBOenS1WQBO8kW0vGw2gv2AyOQ CXZtjXCH/zm6wChzsG3kLmUD0iTmN0xkMJ6g7NNkaqybyQdXWN1B/bVR55sY5CNhkBh/ avspAzEJGwr1Q5khJ+ao+BI7kGPJZ67brdK/05tRW8EcVXq5kSESUYG29OZ2MugTjMHz B7PxmAg20fbhTuCE4JXblsPIzs2vdUVb2aoF/ty7SGXUGxlCpAUkcAmIm8CktHapAVOh hdCA== X-Received: by 10.49.107.226 with SMTP id hf2mr36639544qeb.17.1380121744165; Wed, 25 Sep 2013 08:09:04 -0700 (PDT) Sender: Ned Batchelder Date: Wed, 25 Sep 2013 11:09:02 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: John Gordon Subject: Re: Referrer key missing form os.environ dictionary? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 15 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380121751 news.xs4all.nl 16001 [2001:888:2000:d::a6]:55484 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54743 On 9/25/13 10:26 AM, John Gordon wrote: > You could try this: > > try: > referer = os.environ.get('HTTP_REFERER', 'UnknownRef') > except KeyError: > referer = None > > if not referer: > referer = 'UnknownRef' There's no need for the "except KeyError" clause. dict.get never raises KeyError, this code will assign 'UnknownRef' in the case the environment variable is missing. --Ned.