Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54728 > unrolled thread
| Started by | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-09-25 15:45 +0300 |
| Last post | 2013-09-25 09:58 -0500 |
| Articles | 20 on this page of 35 — 13 participants |
Back to article view | Back to comp.lang.python
Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 15:45 +0300
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 16:14 +0300
Re: Referrer key missing form os.environ dictionary? Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-09-25 16:01 +0200
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 17:04 +0300
Re: Referrer key missing form os.environ dictionary? John Gordon <gordon@panix.com> - 2013-09-25 14:26 +0000
Re: Referrer key missing form os.environ dictionary? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-25 15:04 +0000
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 18:16 +0300
Re: Referrer key missing form os.environ dictionary? Tim Chase <python.list@tim.thechases.com> - 2013-09-25 10:42 -0500
Re: Referrer key missing form os.environ dictionary? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-25 21:17 +0000
Re: Referrer key missing form os.environ dictionary? Ned Batchelder <ned@nedbatchelder.com> - 2013-09-25 11:09 -0400
Re: Referrer key missing form os.environ dictionary? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-25 14:52 +0000
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 18:02 +0300
Re: Referrer key missing form os.environ dictionary? Xaxa Urtiz <urtizvereaxaxa@gmail.com> - 2013-09-25 08:09 -0700
Re: Referrer key missing form os.environ dictionary? Tim Chase <python.list@tim.thechases.com> - 2013-09-25 10:14 -0500
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 18:35 +0300
Re: Referrer key missing form os.environ dictionary? Grant Edwards <invalid@invalid.invalid> - 2013-09-25 15:18 +0000
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 18:41 +0300
Re: Referrer key missing form os.environ dictionary? John Gordon <gordon@panix.com> - 2013-09-25 16:27 +0000
Re: Referrer key missing form os.environ dictionary? Grant Edwards <invalid@invalid.invalid> - 2013-09-25 18:37 +0000
Re: Referrer key missing form os.environ dictionary? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-26 00:11 +0000
Re: Referrer key missing form os.environ dictionary? Terry Reedy <tjreedy@udel.edu> - 2013-09-25 21:32 -0400
Re: Referrer key missing form os.environ dictionary? Chris Angelico <rosuav@gmail.com> - 2013-09-26 13:25 +1000
Re: Referrer key missing form os.environ dictionary? Grant Edwards <invalid@invalid.invalid> - 2013-09-26 14:16 +0000
Re: Referrer key missing form os.environ dictionary? Robert Kern <robert.kern@gmail.com> - 2013-09-26 11:15 +0100
Re: Referrer key missing form os.environ dictionary? Grant Edwards <invalid@invalid.invalid> - 2013-09-26 14:11 +0000
Re: Referrer key missing form os.environ dictionary? Chris Angelico <rosuav@gmail.com> - 2013-09-27 01:17 +1000
Re: Referrer key missing form os.environ dictionary? Grant Edwards <invalid@invalid.invalid> - 2013-09-26 23:27 +0000
Re: Referrer key missing form os.environ dictionary? Chris Angelico <rosuav@gmail.com> - 2013-09-27 16:01 +1000
Re: Referrer key missing form os.environ dictionary? Νίκος <nikos.gr33k@gmail.com> - 2013-09-25 17:07 +0300
Re: Referrer key missing form os.environ dictionary? Robert Kern <robert.kern@gmail.com> - 2013-09-25 15:26 +0100
Re: Referrer key missing form os.environ dictionary? John Gordon <gordon@panix.com> - 2013-09-25 14:30 +0000
Re: Referrer key missing form os.environ dictionary? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-25 14:46 +0000
Re: Referrer key missing form os.environ dictionary? Piet van Oostrum <piet@vanoostrum.org> - 2013-09-25 15:59 -0400
Re: Referrer key missing form os.environ dictionary? John Gordon <gordon@panix.com> - 2013-09-25 14:18 +0000
Re: Referrer key missing form os.environ dictionary? Tim Chase <python.list@tim.thechases.com> - 2013-09-25 09:58 -0500
Page 1 of 2 [1] 2 Next page →
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 15:45 +0300 |
| Subject | Referrer key missing form os.environ dictionary? |
| Message-ID | <l1ulsv$4mv$1@dont-email.me> |
Hello, i decided am ong other os.environ variables to also grab the
'HTTP_REFERER' fiel but when i try to run my script i was seeing a
KeyError complaining that 'HTTP_REFERER' didnt exist.
So, to see what existed in the os.environ dictionary i issues a print(
os.environ ) to see all available keys and their values:
environ({'SERVER_PORT': '80', 'REQUEST_URI': '/',
'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'SERVER_ADDR':
'84.200.17.58', 'DOCUMENT_ROOT': '/home/nikos/public_html',
'HTTP_CONNECTION': 'keep-alive', 'SCRIPT_FILENAME':
'/home/nikos/public_html/cgi-bin/metrites.py', 'SERVER_NAME':
'superhost.gr', 'REMOTE_PORT': '58896', 'HTTP_ACCEPT':
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.2; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.2
Safari/537.36', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,el;q=0.6',
'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_HOST': 'superhost.gr',
'REDIRECT_URL': '/index.html', 'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SIGNATURE': '
Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.0-fips
mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server
at superhost.gr Port 80
\n', 'HTTP_DNT': '1', 'REQUEST_METHOD': 'GET', 'QUERY_STRING':
'file=/home/nikos/public_html/index.html', 'PATH':
'/sbin:/usr/sbin:/bin:/usr/bin', 'HTTP_COOKIE': 'cf_use_ob=0;
__cfduid=da37079bb377f13e9c50224189ab46ac71379783691866;
__utma=210786583.1207352568.1379497319.1380099225.1380108255.32;
__utmc=210786583;
__utmz=210786583.1379497319.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
'REMOTE_ADDR': '176.92.73.41', 'REDIRECT_QUERY_STRING':
'file=/home/nikos/public_html/index.html', 'SERVER_SOFTWARE':
'Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.0-fips
mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635',
'SERVER_ADMIN': 'webmaster@superhost.gr', 'SCRIPT_NAME':
'/cgi-bin/metrites.py', 'REDIRECT_STATUS': '200'})
i dont see anywhere a refferer key so to catch it in a variable sting
like this:
referrer = os.environ['HTTP_REFERER']
Do i miss something? its a suprise to me that the environ dictioanry has
almost anythign but a referrer key.
I need your help please.
[toc] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 16:14 +0300 |
| Message-ID | <l1unj1$dem$1@dont-email.me> |
| In reply to | #54728 |
Στις 25/9/2013 3:45 μμ, ο/η Νίκος έγραψε:
> Hello, i decided am ong other os.environ variables to also grab the
> 'HTTP_REFERER' fiel but when i try to run my script i was seeing a
> KeyError complaining that 'HTTP_REFERER' didnt exist.
>
> So, to see what existed in the os.environ dictionary i issues a print(
> os.environ ) to see all available keys and their values:
>
> environ({'SERVER_PORT': '80', 'REQUEST_URI': '/',
> 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'SERVER_ADDR':
> '84.200.17.58', 'DOCUMENT_ROOT': '/home/nikos/public_html',
> 'HTTP_CONNECTION': 'keep-alive', 'SCRIPT_FILENAME':
> '/home/nikos/public_html/cgi-bin/metrites.py', 'SERVER_NAME':
> 'superhost.gr', 'REMOTE_PORT': '58896', 'HTTP_ACCEPT':
> 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
> 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.2; WOW64)
> AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.2
> Safari/537.36', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,el;q=0.6',
> 'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_HOST': 'superhost.gr',
> 'REDIRECT_URL': '/index.html', 'SERVER_PROTOCOL': 'HTTP/1.1',
> 'SERVER_SIGNATURE': '
> Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.0-fips
> mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server
> at superhost.gr Port 80
> \n', 'HTTP_DNT': '1', 'REQUEST_METHOD': 'GET', 'QUERY_STRING':
> 'file=/home/nikos/public_html/index.html', 'PATH':
> '/sbin:/usr/sbin:/bin:/usr/bin', 'HTTP_COOKIE': 'cf_use_ob=0;
> __cfduid=da37079bb377f13e9c50224189ab46ac71379783691866;
> __utma=210786583.1207352568.1379497319.1380099225.1380108255.32;
> __utmc=210786583;
> __utmz=210786583.1379497319.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
> 'REMOTE_ADDR': '176.92.73.41', 'REDIRECT_QUERY_STRING':
> 'file=/home/nikos/public_html/index.html', 'SERVER_SOFTWARE':
> 'Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.0-fips
> mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635',
> 'SERVER_ADMIN': 'webmaster@superhost.gr', 'SCRIPT_NAME':
> '/cgi-bin/metrites.py', 'REDIRECT_STATUS': '200'})
>
>
> i dont see anywhere a refferer key so to catch it in a variable sting
> like this:
>
> referrer = os.environ['HTTP_REFERER']
>
> Do i miss something? its a suprise to me that the environ dictioanry has
> almost anythign but a referrer key.
>
> I need your help please.
I also tried this:
referer = os.environ.get('HTTP_REFERER', 'UnknownRef')
but that doesn't return anythign either.
[toc] | [prev] | [next] | [standalone]
| From | Chris “Kwpolska” Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2013-09-25 16:01 +0200 |
| Message-ID | <mailman.315.1380117704.18130.python-list@python.org> |
| In reply to | #54728 |
On Wed, Sep 25, 2013 at 2:45 PM, Νίκος <nikos.gr33k@gmail.com> wrote: > Hello, i decided am ong other os.environ variables to also grab the > 'HTTP_REFERER' fiel but when i try to run my script i was seeing a KeyError > complaining that 'HTTP_REFERER' didnt exist. > > So, to see what existed in the os.environ dictionary i issues a print( > os.environ ) to see all available keys and their values: The Referer header is not mandatory by any means. Your client probably does not send it. -- Chris “Kwpolska” Warrick <http://kwpolska.tk> PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 17:04 +0300 |
| Message-ID | <l1uqi7$qi5$1@dont-email.me> |
| In reply to | #54731 |
Στις 25/9/2013 5:01 μμ, ο/η Chris “Kwpolska” Warrick έγραψε:
> On Wed, Sep 25, 2013 at 2:45 PM, Νίκος <nikos.gr33k@gmail.com> wrote:
>> Hello, i decided am ong other os.environ variables to also grab the
>> 'HTTP_REFERER' fiel but when i try to run my script i was seeing a KeyError
>> complaining that 'HTTP_REFERER' didnt exist.
>>
>> So, to see what existed in the os.environ dictionary i issues a print(
>> os.environ ) to see all available keys and their values:
>
> The Referer header is not mandatory by any means. Your client
> probably does not send it.
>
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.
Can you verify that its the correct way to grab the referral string?
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-25 14:26 +0000 |
| Message-ID | <l1urqe$4mm$2@reader1.panix.com> |
| In reply to | #54732 |
In <l1uqi7$qi5$1@dont-email.me> =?UTF-8?B?zp3Or866zr/Pgg==?= <nikos.gr33k@gmail.com> writes:
> referer = os.environ.get('HTTP_REFERER', 'UnknownRef')
> but that doesn't return anything either.
When you say it "doesn't return anything", what exactly do you mean? Does
it return None? Does it raise KeyError? Something else?
In any case, I'm surprised that doesn't work. That should be the correct
way to do it.
You could try this:
try:
referer = os.environ.get('HTTP_REFERER', 'UnknownRef')
except KeyError:
referer = None
if not referer:
referer = 'UnknownRef'
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-25 15:04 +0000 |
| Message-ID | <5242fb8d$0$30000$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #54735 |
On Wed, 25 Sep 2013 14:26:23 +0000, John Gordon wrote:
> You could try this:
>
> try:
> referer = os.environ.get('HTTP_REFERER', 'UnknownRef')
> except KeyError:
> referer = None
> if not referer:
> referer = 'UnknownRef'
The get method will not raise KeyError. If the key is missing,
'UnknownRef' will be returned instead.
So when Nikos reports that os.environ.get('HTTP_REFERER', 'UnknownRef')
"doesn't return anything", he is mistaken. It returns 'UnknownRef'.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 18:16 +0300 |
| Message-ID | <l1uuon$n5e$1@dont-email.me> |
| In reply to | #54742 |
Στις 25/9/2013 6:04 μμ, ο/η Steven D'Aprano έγραψε:
> On Wed, 25 Sep 2013 14:26:23 +0000, John Gordon wrote:
>
>> You could try this:
>>
>> try:
>> referer = os.environ.get('HTTP_REFERER', 'UnknownRef')
>> except KeyError:
>> referer = None
>> if not referer:
>> referer = 'UnknownRef'
>
> The get method will not raise KeyError. If the key is missing,
> 'UnknownRef' will be returned instead.
>
> So when Nikos reports that os.environ.get('HTTP_REFERER', 'UnknownRef')
> "doesn't return anything", he is mistaken. It returns 'UnknownRef'.
>
>
Not actually Steven,
the referer key works well but it was the simlutaneous "host" issues
that was giving me false understanding. now it works ok?
So you say if the key is missign the get method wotn return an error but
the default string instead. That is nice to hear.
But answer me plz this:
how caom the http_referer thing works ok now but when i just print all
the key listing of .os.environ ket the http_referer key isnt inside?
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-09-25 10:42 -0500 |
| Message-ID | <mailman.322.1380123660.18130.python-list@python.org> |
| In reply to | #54746 |
On 2013-09-25 18:16, Νίκος wrote:
> how caom the http_referer thing works ok now but when i just print
> all the key listing of .os.environ ket the http_referer key isnt
> inside?
Well, first off, it's entirely possible (based on reading that
paragraph) that you typed something wrong.
That said, it depends on whether you're looking in your local
environment, or the CGI environment provided by the server. As a
simple example of what the server-side has/puts in your environment,
you can use this CGI script:
#!/usr/bin/env python
import cgitb; cgitb.enable()
import cgi
import os
import sys
print "Content-Type: text/html\n\n"
print """
<html>
<head>
<title>Test CGI</title>
<style>dt{font-weight: bold;}</style>
</head>
<body>
<dl>"""
for k,v in sorted(os.environ.items()):
print "<dt>%s</dt>" % cgi.escape(k)
print "<dd>%s</dd>" % cgi.escape(v)
print """
</dl>
</body>
</html>"""
Note that, if you go directly to the page, you shouldn't have a
refer[r]er header, while if you arrive at it from some previous page,
you might (modulo the aforementioned chicanery that plugins can
induce)
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-09-25 21:17 +0000 |
| Message-ID | <l1vju1$hhj$2@dont-email.me> |
| In reply to | #54746 |
On Wed, 25 Sep 2013 18:16:39 +0300, Νίκος wrote: > how caom the http_referer thing works ok now but when i just print all > the key listing of .os.environ ket the http_referer key isnt inside? That you do not understand this is caused by your failure to understand the HTTP protocol. You have been told before, this NG is not networking and / or tcp/ip and / or internet protocols 101. Please go and learn about the network protocols you're trying to interact with before you start writing code that interacts with them. The issue you are raising here is not a python issue, it is a network protocols issue that has nothing whatsoever to do with python. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2013-09-25 11:09 -0400 |
| Message-ID | <mailman.318.1380121751.18130.python-list@python.org> |
| In reply to | #54735 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-25 14:52 +0000 |
| Message-ID | <5242f8bc$0$30000$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #54732 |
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.
>
> Can you verify that its the correct way to grab the referral string?
The Referer is not an environment variable. How would your shell know
what URL you were just browsing?
Have you googled for HTTP Referer? Do you understand what it is?
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 18:02 +0300 |
| Message-ID | <l1utuk$hs7$1@dont-email.me> |
| In reply to | #54739 |
Στις 25/9/2013 5:52 μμ, ο/η Steven D'Aprano έγραψε:
> 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.
>>
>> Can you verify that its the correct way to grab the referral string?
>
>
> The Referer is not an environment variable. How would your shell know
> what URL you were just browsing?
>
> Have you googled for HTTP Referer? Do you understand what it is?
>
>
Yes Steven googleign for 2 hours now.
You were rigth about the carret it was a synatx error above which i
missed and i was breakign my head to the wall to understand what was wot
with the 'host' variable. why python doestn detect the exact synxtax
error and ispoitnign to me to another line making me think the error is
elsewhere?
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?
[toc] | [prev] | [next] | [standalone]
| From | Xaxa Urtiz <urtizvereaxaxa@gmail.com> |
|---|---|
| Date | 2013-09-25 08:09 -0700 |
| Message-ID | <f93f6904-1922-42a1-8bda-b66af0e55be0@googlegroups.com> |
| In reply to | #54741 |
Le mercredi 25 septembre 2013 17:02:45 UTC+2, Ferrous Cranus a écrit :
> Στις 25/9/2013 5:52 μμ, ο/η Steven D'Aprano έγραψε:
>
> > 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.
>
> >>
>
> >> Can you verify that its the correct way to grab the referral string?
>
> >
>
> >
>
> > The Referer is not an environment variable. How would your shell know
>
> > what URL you were just browsing?
>
> >
>
> > Have you googled for HTTP Referer? Do you understand what it is?
>
> >
>
> >
>
> Yes Steven googleign for 2 hours now.
>
>
>
> You were rigth about the carret it was a synatx error above which i
>
> missed and i was breakign my head to the wall to understand what was wot
>
> with the 'host' variable. why python doestn detect the exact synxtax
>
> error and ispoitnign to me to another line making me think the error is
>
> elsewhere?
>
>
>
> 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?
http://www.tutorialspoint.com/python/dictionary_get.htm
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-09-25 10:14 -0500 |
| Message-ID | <mailman.319.1380121985.18130.python-list@python.org> |
| In reply to | #54741 |
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
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 18:35 +0300 |
| Message-ID | <l1uvr7$tej$1@dont-email.me> |
| In reply to | #54745 |
Στις 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.
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-09-25 15:18 +0000 |
| Message-ID | <l1uusg$k0t$1@reader1.panix.com> |
| In reply to | #54739 |
On 2013-09-25, Steven D'Aprano <steve+comp.lang.python@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.
--
Grant Edwards grant.b.edwards Yow! Boy, am I glad it's
at only 1971...
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-25 18:41 +0300 |
| Message-ID | <l1v07g$tej$2@dont-email.me> |
| In reply to | #54747 |
Στις 25/9/2013 6:18 μμ, ο/η Grant Edwards έγραψε:
> On 2013-09-25, Steven D'Aprano <steve+comp.lang.python@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?
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-25 16:27 +0000 |
| Message-ID | <l1v2tb$q2j$1@reader1.panix.com> |
| In reply to | #54750 |
In <l1v07g$tej$2@dont-email.me> =?UTF-8?B?zp3Or866zr/Pgg==?= <nikos.gr33k@gmail.com> writes:
> 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. Each process has its own environment. Your shell's environment
knows nothing about the web server's environment.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-09-25 18:37 +0000 |
| Message-ID | <l1vah7$nd8$1@reader1.panix.com> |
| In reply to | #54750 |
On 2013-09-25, ?????????? <nikos.gr33k@gmail.com> wrote:
> ???????? 25/9/2013 6:18 ????, ??/?? Grant Edwards ????????????:
>>> 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
If the shell was started by the HTTP server, yes. If you logged in
normally, no.
> because it happens to be in the same enviroment with the HTTP server?
The shell will only have that environment if the shell was run by the
HTTP server.
> No need to run it via web browser and check the Apache's error log?
You can set the environemnt variables appropriately in the shell and
then invoke a CGI application directly for testing purposes.
--
Grant Edwards grant.b.edwards Yow! I was born in a
at Hostess Cupcake factory
gmail.com before the sexual
revolution!
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-26 00:11 +0000 |
| Message-ID | <52437bca$0$30000$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #54747 |
On Wed, 25 Sep 2013 15:18:41 +0000, Grant Edwards wrote: >> 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. I stand corrected. That's a pretty shitty design though, isn't it? Communicating via environment variables. What is this, 1998? :-) Mind you, I'm not sure what other alternatives exist. -- Steven
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web