Re: Problem checking an existing browser cookie
On 30 Αύγ, 04:51, MRAB wrote: > On 30/08/2010 02:14, Νίκος wrote: > > > > > > > > > > > On 29 Αύγ, 21:44, MRAB wrote: > >> On 29/08/2010 06:34, Νίκος wrote: > > >>> On 28 Αύγ, 23:15, MRAB wrote: > On 28/08/2010 20:37, Íßêïò wrote: > > > On 22 Áýã, 10:27, Íßêïò wrote: > >> On 16 Áýã, 14:31, Peter Otten<__pete...@web.de> wrote: > > >>> Íßêïò wrote: > # initializecookie > cookie=Cookie.SimpleCookie() > cookie.load( os.environ.get('HTTP_COOKIE', '') ) > mycookie =cookie.get('visitor') > > if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta| > yandex|13448|spider|crawl)', host ) is None: > blabla... > > > I checked and Chrome has acookienames visitor with a value ofnikos > within. > So, i have to ask why the if fails? > > >>> Maybe it's because != != == > > >> Iwant ti if code block to be executed only if the browsercookienames > >> visitor fetched doesnt cotnain the vbalue of 'nikos' > > >> Is there somethign wrong with the way i wrote it? > > > Please do help me with this too becaus eif i dont solve this my > > website keeps count my each visit like iam a guest visitor! > > Print out mycookie, repr(mycookie.value) (unless mycookie is None) and > repr(host). Then follow the code yourself to see whether the condition > is True. > > >>> print mycookie outputs 'None' > > >>> Thts weird because i check with the browser and the cookie is there! > > >> Just because you can see it doesn't mean your code can. > > >>> print repr(host) outputs '78-236-176.adsl.cyta.gr' > > >>> repr(mycookie.value) (unless mycookie is None) > > >>> and also > > >>> print mycookie.value gives an error too. Maybe there is not a value > >>> method? > > >> If mycookie is None, then it's not surprising that doesn't have 'value'. > > >> In summary, mycookie is None, so: > > >> mycookie and mycookie.value != 'nikos' > > >> is false (actually None, which is treated as false). > > >> host == '78-236-176.adsl.cyta.gr', so: > > >> re.search(r'(cyta|yandex|13448|spider|crawl)', host) > > >> finds 'cyta' and the search returns a match. > > >> false or false == false > > >> blabla... isn't executed. > > > Lets forget the 2nd or argument, ill put it off > > > so we have this now > > > if ( mycookie and mycookie.value != 'nikos' ): > > #do stuff as long as there ins't a cookie names visitor with a > > value of nikos in the broswer > > > What does it mean practically that the mycookie equals to None? > > That mycookie doesnt exist? > > > How should i write this if block to mkake sure it checks whether or > > not the cookie exists? > > Under what conditions do you want to execute the block? > > This: > > mycookie and mycookie.value != 'nikos' > > will be true if: > > there _is_ a cookie, but its value isn't 'nikos' > > I think that you want is to execute the block if someone else is > visiting. Correct? Yes that exactyl right! To make sure a cookie is set i have a script names koukos.py containing this: == #!/usr/bin/python # -*- coding: utf-8 -*- import cgitb; cgitb.enable() import cgi, os, Cookie # initialize cookie cookie = Cookie.SimpleCookie() cookie.load( os.environ.get('HTTP_COOKIE', '') ) mycookie = cookie.get('visitor') htmlBody = [] # if visitor cookie does exist if ( mycookie and mycookie.value == 'nikos' ): htmlBody.append('ΑΠΟ ΤΗΝ ΕΠΟΜΕΝΗ ΕΠΙΣΚΕΨΗ ΣΟΥ ΘΑ ΣΕ ΥΠΟΛΟΓΙΖΩ ΩΣ ΕΠΙΣΚΕΠΤΗ ΑΥΞΑΝΟΝΤΑΣ ΤΟΝ ΜΕΤΡΗΤΗ!') cookie['visitor'] = 'nikos' cookie['visitor']['expires'] = -1 #this cookie will expire now else: htmlBody.append('ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞΕΡΩ, ΔΕΝ ΣΕ ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΝ Ο ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!!') cookie['visitor'] = 'nikos' cookie['visitor']['expires'] = 60*60*24*30*12 htmlBody.insert(0, 'Content-type: text/html; charset=UTF-8\n') print(cookie) print('\n'.join(htmlBody)) = Which i seicth on and off according to my desire if i want to be counted or not! > How do you know when it _is_ you? There'll be a cookie which says it's > you? > > If so, then you want to execute the block if there isn't any cookie, or > if there's a cookie but it doesn't say it's you: > > not mycookie or mycookie.value != 'nikos' i tried this as you suggested: if ( not mycookie or mycookie.value != 'nikos' ) or re.search( r'(msn| yandex|13448|spider|crawl)', host ) is None: but the counter keeps increasing although the cookie named visitor on my browser exist and also has the valuie of 'nikos'. Why it keeps increasing? Doesn't the if code "sees" that the cookie with a value of "nikos" is present!?!! -- http://mail.python.org/mailman/listinfo/python-list
Re: String substitution VS proper mysql escaping
On 30 Αύγ, 05:04, MRAB wrote: when iam trying to pass a tuple to the execute methos should i pass it like this? cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = %s and host = %s ''' % (page, date, host) ) or like tuple = (page, host, date) cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = %s and host = %s ''' % (tuple) ) Or is it the same thing? > > = > > I'm asking this to see why > > > cursor.execute(''' SELECT hits FROM counters WHERE page = '%s' and > > date = '%s' and host = '%s' ''' % (page, date, host) ) > > > does work, while same thign qithout the quotes > > > cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date > > = %s and host = %s ''' % (page, date, host) ) > > > doesn't. Dont know why but quotes somehopw confuse me both in strings > > and sql_queries as well when it comes to substitutions. > > Don't quote the placeholders yourself. Let the method do it. No, iam taking substitution here not mysql escaping. Cursor.execute(''' SELECT hits FROM counters WHERE page = '%s' and date = '%s' and host = '%s' ''' % (page, date, host) ) As it is above it works , with double quotes still works but if i leave it unquoted it doesn't. This is because without sigle or double quotes the the method doesn't know where a value begins and here it ends? That why it needs quoting? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 05:43, MRAB wrote: > On 30/08/2010 03:07, Nik the Greek wrote: > > > > > > > > > > > On 30 Αύγ, 04:51, MRAB wrote: > >> On 30/08/2010 02:14, Νίκος wrote: > > >>> On 29 Αύγ, 21:44, MRAB wrote: > >>>> On 29/08/2010 06:34, Νίκος wrote: > > >>>>> On 28 Αύγ, 23:15, MRAB wrote: > >>>>>> On 28/08/2010 20:37, Íßêïò wrote: > > >>>>>>> On 22 Áýã, 10:27, Íßêïò wrote: > >>>>>>>> On 16 Áýã, 14:31, Peter Otten<__pete...@web.de> wrote: > > >>>>>>>>> Íßêïò wrote: > >>>>>>>>>> # initializecookie > >>>>>>>>>> cookie=Cookie.SimpleCookie() > >>>>>>>>>> cookie.load( os.environ.get('HTTP_COOKIE', '') ) > >>>>>>>>>> mycookie =cookie.get('visitor') > > >>>>>>>>>> if ( mycookie and mycookie.value != 'nikos' ) or re.search( > >>>>>>>>>> r'(cyta| > >>>>>>>>>> yandex|13448|spider|crawl)', host ) is None: > >>>>>>>>>> blabla... > >>>>>>>>>> > > >>>>>>>>>> I checked and Chrome has acookienames visitor with a value ofnikos > >>>>>>>>>> within. > >>>>>>>>>> So, i have to ask why the if fails? > > >>>>>>>>> Maybe it's because != != == > > >>>>>>>> Iwant ti if code block to be executed only if the browsercookienames > >>>>>>>> visitor fetched doesnt cotnain the vbalue of 'nikos' > > >>>>>>>> Is there somethign wrong with the way i wrote it? > > >>>>>>> Please do help me with this too becaus eif i dont solve this my > >>>>>>> website keeps count my each visit like iam a guest visitor! > > >>>>>> Print out mycookie, repr(mycookie.value) (unless mycookie is None) and > >>>>>> repr(host). Then follow the code yourself to see whether the condition > >>>>>> is True. > > >>>>> print mycookie outputs 'None' > > >>>>> Thts weird because i check with the browser and the cookie is there! > > >>>> Just because you can see it doesn't mean your code can. > > >>>>> print repr(host) outputs '78-236-176.adsl.cyta.gr' > > >>>>> repr(mycookie.value) (unless mycookie is None) > > >>>>> and also > > >>>>> print mycookie.value gives an error too. Maybe there is not a value > >>>>> method? > > >>>> If mycookie is None, then it's not surprising that doesn't have 'value'. > > >>>> In summary, mycookie is None, so: > > >>>> mycookie and mycookie.value != 'nikos' > > >>>> is false (actually None, which is treated as false). > > >>>> host == '78-236-176.adsl.cyta.gr', so: > > >>>> re.search(r'(cyta|yandex|13448|spider|crawl)', host) > > >>>> finds 'cyta' and the search returns a match. > > >>>> false or false == false > > >>>> blabla... isn't executed. > > >>> Lets forget the 2nd or argument, ill put it off > > >>> so we have this now > > >>> if ( mycookie and mycookie.value != 'nikos' ): > >>> #do stuff as long as there ins't a cookie names visitor with a > >>> value of nikos in the broswer > > >>> What does it mean practically that the mycookie equals to None? > >>> That mycookie doesnt exist? > > >>> How should i write this if block to mkake sure it checks whether or > >>> not the cookie exists? > > >> Under what conditions do you want to execute the block? > > >> This: > > >> mycookie and mycookie.value != 'nikos' > > >> will be true if: > > >> there _is_ a cookie, but its value isn't 'nikos' > > >> I think that you want is to execute the block if someone else is > >> visiting. Correct? > > > Yes that exactyl right! > > > To make sure a cookie is set i have a script names koukos.py > > conta
Re: String substitution VS proper mysql escaping
On 30 Αύγ, 05:48, MRAB wrote: > On 30/08/2010 03:33, Nik the Greek wrote: > > > > > > > > > On 30 Αύγ, 05:04, MRAB wrote: > > > when iam trying to pass a tuple to the execute methos should i pass it > > like this? > > > cursor.execute(''' SELECT hits FROM counters WHERE page = %s and > > date = %s and host = %s ''' % (page, date, host) ) > > > or like > > > tuple = (page, host, date) > > > cursor.execute(''' SELECT hits FROM counters WHERE page = %s and > > date = %s and host = %s ''' % (tuple) ) > > > Or is it the same thing? > > 'tuple' is the name of a built-in. Don't use it. > > The first example is clearer. ok a_tuple = (page, hist, host) cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = %s and host = %s ''' , a_tuple ) would that syntax be correct? No need to enclose the tuple name inside parenthesis here right? > >>> = > >>> I'm asking this to see why > > >>> cursor.execute(''' SELECT hits FROM counters WHERE page = '%s' and > >>> date = '%s' and host = '%s' ''' % (page, date, host) ) > > >>> does work, while same thign qithout the quotes > > >>> cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date > >>> = %s and host = %s ''' % (page, date, host) ) > > >>> doesn't. Dont know why but quotes somehopw confuse me both in strings > >>> and sql_queries as well when it comes to substitutions. > > >> Don't quote the placeholders yourself. Let the method do it. > > > No, iam taking substitution here not mysql escaping. > > > Cursor.execute(''' SELECT hits FROM counters WHERE page = '%s' and > > date = '%s' and host = '%s' ''' % (page, date, host) ) > > > As it is above it works , with double quotes still works but if i > > leave it unquoted it doesn't. > > > This is because without sigle or double quotes the the method doesn't > > know where a value begins and here it ends? That why it needs quoting? > > Let the method do the substitution: > > cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = > %s and host = %s ''', (page, date, host) ) > > This is the best way. Yes i will i just asked to know if i were to substitute what might be the problem so to understand why i need the quoting. why not like that? > cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = > %s and host = %s ''' % (page, date, host) ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 06:12, MRAB wrote: > This part: > > ( not mycookie or mycookie.value != 'nikos' ) > > is false but this part: > > re.search( r'(msn|yandex|13448|spider|crawl)', host ) is None > > is true because host doesn't contain any of those substrings. So, the if code does executed because one of the condition is true? How should i write it? I cannot think clearly on this at all. I just wan to tell it to get executed ONLY IF the cookie values is not 'nikos' or ( don't knwo if i have to use and or 'or' here) host does not contain any of the substrings. What am i doign wrong?! -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 06:12, MRAB wrote: > On 30/08/2010 03:55, Nik the Greek wrote: > > > > > > > > > > > On 30 Αύγ, 05:43, MRAB wrote: > >> On 30/08/2010 03:07, Nik the Greek wrote: > > >>> On 30 Αύγ, 04:51, MRAB wrote: > >>>> On 30/08/2010 02:14, Νίκος wrote: > > >>>>> On 29 Αύγ, 21:44, MRAB wrote: > >>>>>> On 29/08/2010 06:34, Νίκος wrote: > > >>>>>>> On 28 Αύγ, 23:15, MRAB wrote: > >>>>>>>> On 28/08/2010 20:37, Íßêïò wrote: > > >>>>>>>>> On 22 Áýã, 10:27, Íßêïò wrote: > >>>>>>>>>> On 16 Áýã, 14:31, Peter Otten<__pete...@web.de> wrote: > > >>>>>>>>>>> Íßêïò wrote: > >>>>>>>>>>>> # initializecookie > >>>>>>>>>>>> cookie=Cookie.SimpleCookie() > >>>>>>>>>>>> cookie.load( os.environ.get('HTTP_COOKIE', '') ) > >>>>>>>>>>>> mycookie =cookie.get('visitor') > > >>>>>>>>>>>> if ( mycookie and mycookie.value != 'nikos' ) or re.search( > >>>>>>>>>>>> r'(cyta| > >>>>>>>>>>>> yandex|13448|spider|crawl)', host ) is None: > >>>>>>>>>>>> blabla... > >>>>>>>>>>>> > > >>>>>>>>>>>> I checked and Chrome has acookienames visitor with a value > >>>>>>>>>>>> ofnikos > >>>>>>>>>>>> within. > >>>>>>>>>>>> So, i have to ask why the if fails? > > >>>>>>>>>>> Maybe it's because != != == > > >>>>>>>>>> Iwant ti if code block to be executed only if the > >>>>>>>>>> browsercookienames > >>>>>>>>>> visitor fetched doesnt cotnain the vbalue of 'nikos' > > >>>>>>>>>> Is there somethign wrong with the way i wrote it? > > >>>>>>>>> Please do help me with this too becaus eif i dont solve this my > >>>>>>>>> website keeps count my each visit like iam a guest visitor! > > >>>>>>>> Print out mycookie, repr(mycookie.value) (unless mycookie is None) > >>>>>>>> and > >>>>>>>> repr(host). Then follow the code yourself to see whether the > >>>>>>>> condition > >>>>>>>> is True. > > >>>>>>> print mycookie outputs 'None' > > >>>>>>> Thts weird because i check with the browser and the cookie is there! > > >>>>>> Just because you can see it doesn't mean your code can. > > >>>>>>> print repr(host) outputs '78-236-176.adsl.cyta.gr' > > >>>>>>> repr(mycookie.value) (unless mycookie is None) > > >>>>>>> and also > > >>>>>>> print mycookie.value gives an error too. Maybe there is not a value > >>>>>>> method? > > >>>>>> If mycookie is None, then it's not surprising that doesn't have > >>>>>> 'value'. > > >>>>>> In summary, mycookie is None, so: > > >>>>>> mycookie and mycookie.value != 'nikos' > > >>>>>> is false (actually None, which is treated as false). > > >>>>>> host == '78-236-176.adsl.cyta.gr', so: > > >>>>>> re.search(r'(cyta|yandex|13448|spider|crawl)', host) > > >>>>>> finds 'cyta' and the search returns a match. > > >>>>>> false or false == false > > >>>>>> blabla... isn't executed. > > >>>>> Lets forget the 2nd or argument, ill put it off > > >>>>> so we have this now > > >>>>> if ( mycookie and mycookie.value != 'nikos' ): > >>>>> #do stuff as long as there ins't a cookie names visitor with a > >>>>> value of nikos in the broswer > > >>>>> What does it mean practically that the my
Re: Problem checking an existing browser cookie
On 30 Αύγ, 11:01, Nik the Greek wrote: > On 30 Αύγ, 06:12, MRAB wrote: > > > > > > > > > > > On 30/08/2010 03:55, Nik the Greek wrote: > > > > On 30 Αύγ, 05:43, MRAB wrote: > > >> On 30/08/2010 03:07, Nik the Greek wrote: > > > >>> On 30 Αύγ, 04:51, MRAB wrote: > > >>>> On 30/08/2010 02:14, Νίκος wrote: > > > >>>>> On 29 Αύγ, 21:44, MRAB wrote: > > >>>>>> On 29/08/2010 06:34, Νίκος wrote: > > > >>>>>>> On 28 Αύγ, 23:15, MRAB wrote: > > >>>>>>>> On 28/08/2010 20:37, Íßêïò wrote: > > > >>>>>>>>> On 22 Áýã, 10:27, Íßêïò wrote: > > >>>>>>>>>> On 16 Áýã, 14:31, Peter Otten<__pete...@web.de> wrote: > > > >>>>>>>>>>> Íßêïò wrote: > > >>>>>>>>>>>> # initializecookie > > >>>>>>>>>>>> cookie=Cookie.SimpleCookie() > > >>>>>>>>>>>> cookie.load( os.environ.get('HTTP_COOKIE', '') ) > > >>>>>>>>>>>> mycookie =cookie.get('visitor') > > > >>>>>>>>>>>> if ( mycookie and mycookie.value != 'nikos' ) or re.search( > > >>>>>>>>>>>> r'(cyta| > > >>>>>>>>>>>> yandex|13448|spider|crawl)', host ) is None: > > >>>>>>>>>>>> blabla... > > >>>>>>>>>>>> > > > >>>>>>>>>>>> I checked and Chrome has acookienames visitor with a value > > >>>>>>>>>>>> ofnikos > > >>>>>>>>>>>> within. > > >>>>>>>>>>>> So, i have to ask why the if fails? > > > >>>>>>>>>>> Maybe it's because != != == > > > >>>>>>>>>> Iwant ti if code block to be executed only if the > > >>>>>>>>>> browsercookienames > > >>>>>>>>>> visitor fetched doesnt cotnain the vbalue of 'nikos' > > > >>>>>>>>>> Is there somethign wrong with the way i wrote it? > > > >>>>>>>>> Please do help me with this too becaus eif i dont solve this my > > >>>>>>>>> website keeps count my each visit like iam a guest visitor! > > > >>>>>>>> Print out mycookie, repr(mycookie.value) (unless mycookie is None) > > >>>>>>>> and > > >>>>>>>> repr(host). Then follow the code yourself to see whether the > > >>>>>>>> condition > > >>>>>>>> is True. > > > >>>>>>> print mycookie outputs 'None' > > > >>>>>>> Thts weird because i check with the browser and the cookie is there! > > > >>>>>> Just because you can see it doesn't mean your code can. > > > >>>>>>> print repr(host) outputs '78-236-176.adsl.cyta.gr' > > > >>>>>>> repr(mycookie.value) (unless mycookie is None) > > > >>>>>>> and also > > > >>>>>>> print mycookie.value gives an error too. Maybe there is not a value > > >>>>>>> method? > > > >>>>>> If mycookie is None, then it's not surprising that doesn't have > > >>>>>> 'value'. > > > >>>>>> In summary, mycookie is None, so: > > > >>>>>> mycookie and mycookie.value != 'nikos' > > > >>>>>> is false (actually None, which is treated as false). > > > >>>>>> host == '78-236-176.adsl.cyta.gr', so: > > > >>>>>> re.search(r'(cyta|yandex|13448|spider|crawl)', host) > > > >>>>>> finds 'cyta' and the search returns a match. > > > >>>>>> false or false == false > > > >>>>>> blabla... isn't executed. > > > >>>>> Lets forget the 2nd or argument, ill put it off > > > >>>>>
Re: String substitution VS proper mysql escaping
On 30 Αύγ, 11:11, Gregory Ewing wrote: > Nik the Greek wrote: > > Yes i will i just asked to know if i were to substitute what might be > > the problem so to understand why i need the quoting. > > Because if you use % to build a query string, the result must > be syntactically valid SQL. The values that you substitute > into the placeholders must end up looking like SQL literals. > That means string values need to be in quotes, and probably > dates as well, although numbers don't. > > When you use the execute method's own parameter substitution > mechanism, things are different. It's not a textual replacement, > and you don't put quotes around the placeholders. There's no > particular reason for that, it's just the way it's defined > to work. > > -- > Greg cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = %s and host = %s ''' , a_tuple ) and cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = %s and host = %s ''' , (a_tuple) ) are both syntactically correct right? buw what about cursor.execute(''' SELECT hits FROM counters WHERE page = %s and date = %s and host = %s ''' , (a_tuple,) ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 19:41, MRAB wrote: > On 30/08/2010 04:33, Nik the Greek wrote: > > > > > > > > > On 30 Αύγ, 06:12, MRAB wrote: > > >> This part: > > >> ( not mycookie or mycookie.value != 'nikos' ) > > >> is false but this part: > > >> re.search( r'(msn|yandex|13448|spider|crawl)', host ) is None > > >> is true because host doesn't contain any of those substrings. > > > So, the if code does executed because one of the condition is true? > > > How should i write it? > > > I cannot think clearly on this at all. > > > I just wan to tell it to get executed ONLY IF > > > the cookie values is not 'nikos' > > > or ( don't knwo if i have to use and or 'or' here) > > > host does not contain any of the substrings. > > > What am i doign wrong?! > > It might be clearer if you reverse the condition and say: > > me_visiting = ... > if not me_visiting: > ... I don't understand what are you trying to say Please provide a full example. You mean i should try it like this? unless ( visitor and visitor.value == 'nikos' ) or re.search( r'(msn| yandex|13448|spider|crawl)', host ) not None: But isnt it the same thing like the if? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 19:21, Peter Otten <__pete...@web.de> wrote: > Nik the Greek wrote: > >> Perhpas its doenst get loaded like that? > > >> # initialize cookie > >> cookie = SimpleCookie() > >> cookie.load( os.environ.get('HTTP_COOKIE', '') ) > >> mycookie = cookie.get('visitor') > > > Please someone else has an idea on how this to work? > > Add a print statement to verify that HTTP_COOKIE does indeed have a visitor. > Or use the stuff below as a template. > > Here is a minimal script to set the visitor: > > #!/usr/bin/env python > import cgitb > cgitb.enable() > > import cgi > import Cookie > import os > import sys > > form = cgi.FieldStorage() > name = form.getfirst("name", "Unknown") > > cookie = Cookie.SimpleCookie() > cookie["visitor"] = name > > sys.stdout.write(cookie.output()) > sys.stdout.write("\r\nContent-type: text/plain\r\n\r\n") > print "Hello, your new name is", name > > Invoke it with the equivalent of > > http://localhost:8000/cgi-bin/set_visitor.py?name=Nikos > > for your website. Then proceed with a small script to show the visitor's > name: > > #!/usr/bin/env python > import cgitb > cgitb.enable() > > import cgi > import Cookie > import os > import sys > > cookie = Cookie.SimpleCookie() > cookie.load(os.environ.get("HTTP_COOKIE")) > > visitor = cookie.get("visitor") > if visitor is None: > visitor_name = "Unknown" > else: > visitor_name = visitor.value > > sys.stdout.write("Content-type: text/plain\r\n\r\n") > print "Hello,", visitor_name > print > print > print "HTTP_COOKIE=%r" % os.environ.get("HTTP_COOKIE") > > which you can invoke with the equivalent of > > http://localhost:8000/cgi-bin/show_visitor.py > > With some luck you should see your name and can proceed to adapt your script > accordingly. > > Peter The cookie is set tyo the browser with the code i provided few posts back The problem is that i for soemreason cant check correctly its existance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 19:41, MRAB wrote: > On 30/08/2010 04:33, Nik the Greek wrote: > > > > > > > > > On 30 Αύγ, 06:12, MRAB wrote: > > >> This part: > > >> ( not mycookie or mycookie.value != 'nikos' ) > > >> is false but this part: > > >> re.search( r'(msn|yandex|13448|spider|crawl)', host ) is None > > >> is true because host doesn't contain any of those substrings. > > > So, the if code does executed because one of the condition is true? > > > How should i write it? > > > I cannot think clearly on this at all. > > > I just wan to tell it to get executed ONLY IF > > > the cookie values is not 'nikos' > > > or ( don't knwo if i have to use and or 'or' here) > > > host does not contain any of the substrings. > > > What am i doign wrong?! > > It might be clearer if you reverse the condition and say: > > me_visiting = ... > if not me_visiting: > ... # initialize cookie cookie = SimpleCookie() cookie.load( os.environ.get('HTTP_COOKIE', '') ) visitor = cookie.get('visitor') This statement if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448| spider|crawl)', host ) is None: produces the following error: /home/webville/public_html/cgi-bin/counter.py 93 # do not increment the counter if a Cookie is set to the visitors browser already 94 # = 95 if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448| spider|crawl)', host ) is None: 96 97 print visitor visitor = None, visitor.value undefined, re = , re.search = , host = '178-128-217.dynamic.cyta.gr', builtin None = None Why visitor.value is undefined? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 20:53, MRAB wrote: > > Why visitor.value is undefined? > > Because visitor is None. It's not seeing any cookie. WHY NOT?! THE COOKIE _DOES_EXIST !!! What am i missing here?! -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 20:48, Rami Chowdhury wrote: > On Mon, Aug 30, 2010 at 23:36, Nik the Greek > wrote: > > > > > > > > > > > # initialize cookie > > cookie = SimpleCookie() > > cookie.load( os.environ.get('HTTP_COOKIE', '') ) > > visitor = cookie.get('visitor') > > > This statement > > > if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448| > > spider|crawl)', host ) is None: > > > produces the following error: > > > /home/webville/public_html/cgi-bin/counter.py > > 93 # do not increment the counter if a Cookie is set to the > > visitors browser already > > 94 # > > === > > == > > 95 if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448| > > spider|crawl)', host ) is None: > > 96 > > 97 print visitor > > visitor = None, visitor.value undefined, re = > lib64/python2.4/re.pyc'>, re.search = , host = > > '178-128-217.dynamic.cyta.gr', builtin None = None > > > Why visitor.value is undefined? > > Because, as the traceback tells you, visitor is None. As you said, the > cookie is not recognized. Try inserting the following line at the top > of your script: > > print "Content-type:text/html\r\n\r\n", os.environ.get('HTTP_COOKIE', > 'NO COOKIE DATA') > > That should give you the value of the cookie, which might help you > debug why it is not being loaded. > > -- > Rami Chowdhury > "Never assume malice when stupidity will suffice." -- Hanlon's Razor > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) Unfortunately it produces an Internal Server Error :( -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 30 Αύγ, 20:50, MRAB wrote: > On 30/08/2010 18:16, Nik the Greek wrote: > > > > > > > > > > > On 30 Αύγ, 19:41, MRAB wrote: > >> On 30/08/2010 04:33, Nik the Greek wrote: > > >>> On 30 Αύγ, 06:12, MRAB wrote: > > >>>> This part: > > >>>> ( not mycookie or mycookie.value != 'nikos' ) > > >>>> is false but this part: > > >>>> re.search( r'(msn|yandex|13448|spider|crawl)', host ) is None > > >>>> is true because host doesn't contain any of those substrings. > > >>> So, the if code does executed because one of the condition is true? > > >>> How should i write it? > > >>> I cannot think clearly on this at all. > > >>> I just wan to tell it to get executed ONLY IF > > >>> the cookie values is not 'nikos' > > >>> or ( don't knwo if i have to use and or 'or' here) > > >>> host does not contain any of the substrings. > > >>> What am i doign wrong?! > > >> It might be clearer if you reverse the condition and say: > > >> me_visiting = ... > >> if not me_visiting: > >> ... > > > I don't understand what are you trying to say > > > Please provide a full example. > > > You mean i should try it like this? > > > unless ( visitor and visitor.value == 'nikos' ) or re.search( r'(msn| > > yandex|13448|spider|crawl)', host ) not None: > > > But isnt it the same thing like the if? > > My point is that the logic might be clearer to you if you think first > about how you know when you _are_ the visitor. Well my idea was to set a cookie on my browser with the name visitor and a value of "nikos" and then check each time that cooki. if value is "nikos" then dont count! I could also pass an extra url string like http://webville.gr?show=nikos and check that but i dont like the idea very much of giving an extra string each time i want to visit my webpage. So form the 2 solution mentioned the 1st one is better but cant come into action for some reason. Aprt form those too solution i cant think of anyhting else that would identify me and filter me out of the actual guest of my website. I'm all ears if you can think of something else. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem checking an existing browser cookie
On 31 Αύγ, 11:07, Nik the Greek wrote: > On 30 Αύγ, 20:50, MRAB wrote: > > > > > > > > > > > On 30/08/2010 18:16, Nik the Greek wrote: > > > > On 30 Αύγ, 19:41, MRAB wrote: > > >> On 30/08/2010 04:33, Nik the Greek wrote: > > > >>> On 30 Αύγ, 06:12, MRAB wrote: > > > >>>> This part: > > > >>>> ( not mycookie or mycookie.value != 'nikos' ) > > > >>>> is false but this part: > > > >>>> re.search( r'(msn|yandex|13448|spider|crawl)', host ) is None > > > >>>> is true because host doesn't contain any of those substrings. > > > >>> So, the if code does executed because one of the condition is true? > > > >>> How should i write it? > > > >>> I cannot think clearly on this at all. > > > >>> I just wan to tell it to get executed ONLY IF > > > >>> the cookie values is not 'nikos' > > > >>> or ( don't knwo if i have to use and or 'or' here) > > > >>> host does not contain any of the substrings. > > > >>> What am i doign wrong?! > > > >> It might be clearer if you reverse the condition and say: > > > >> me_visiting = ... > > >> if not me_visiting: > > >> ... > > > > I don't understand what are you trying to say > > > > Please provide a full example. > > > > You mean i should try it like this? > > > > unless ( visitor and visitor.value == 'nikos' ) or re.search( r'(msn| > > > yandex|13448|spider|crawl)', host ) not None: > > > > But isnt it the same thing like the if? > > > My point is that the logic might be clearer to you if you think first > > about how you know when you _are_ the visitor. > > Well my idea was to set a cookie on my browser with the name visitor > and a value of "nikos" and then check each time that cooki. if value > is "nikos" then dont count! > > I could also pass an extra url string likehttp://webville.gr?show=nikos > and check that but i dont like the idea very much of giving an extra > string each time i want to visit my webpage. > So form the 2 solution mentioned the 1st one is better but cant come > into action for some reason. > > Aprt form those too solution i cant think of anyhting else that would > identify me and filter me out of the actual guest of my website. > > I'm all ears if you can think of something else. Is there any other way for the webpage to identify me and filter me out except checking a cookie or attach an extra url string to the address bar? -- http://mail.python.org/mailman/listinfo/python-list