Problem checking an existing browser cookie

Í

Íßêïò

# initialize cookie
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 a cookie names visitor with a value of nikos
within.
So, i have to ask why the if fails?

Thank you.
 
P

Peter Otten

Îίκος said:
# initialize cookie
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 a cookie names visitor with a value of nikos
within.
So, i have to ask why the if fails?

Maybe it's because != != ==
 
Î

Îίκος

Maybe it's because != != ==

Iwant ti if code block to be executed only if the browser cookie names
visitor fetched doesnt cotnain the vbalue of 'nikos'

Is there somethign wrong with the way i wrote it?
 
Í

Íßêïò

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!
 
P

Peter Otten

Îίκος said:
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!

In your initial post it sounded like you wanted the if-branch to execute for
a user named "nikos", but now it seems that I misunderstood you and swapping
'mycookie.value != "nikos"' for 'mycookie.value == "nikos"' won't help.
Maybe you could add a print statement like

print mycookie.value

to start with your debugging efforts.

Peter
 
M

MRAB

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 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!

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?
 
Î

Îίκος

In your initial post it sounded like you wanted the if-branch to execute for
a user named "nikos", but now it seems that I misunderstood you and swapping
'mycookie.value != "nikos"' for 'mycookie.value == "nikos"' won't help.
Maybe you could add a print statement like

print mycookie.value

to start with your debugging efforts.

Peter
Maybe

# initialize cookie
cookie = Cookie.SimpleCookie()
cookie.load( os.environ.get('HTTP_COOKIE', '') )
mycookie = cookie.get('visitor')

wont load the cookie correctly? because

print mycookie.value

outputs an error.
 
M

MRAB

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.
 
Î

Îίκος

Just because you can see it doesn't mean your code can.





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?
 
M

MRAB

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?

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'
 
N

Nik the Greek

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!?!!
 
M

MRAB

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!?!!

Previously your regex was r'(cyta|yandex|13448|spider|crawl)' and you
said that host was '78-236-176.adsl.cyta.gr', so it matched.

Now your regex is r'(msn|yandex|13448|spider|crawl)'. If host is still
'78-236-176.adsl.cyta.gr' then of course it doesn't match, so the
condition is true.
 
N

Nik the Greek

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')

Which i seicth on and off according to my desire if i want to be
counted or not!
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!?!!

Previously your regex was r'(cyta|yandex|13448|spider|crawl)' and you
said that host was '78-236-176.adsl.cyta.gr', so it matched.

Now your regex is r'(msn|yandex|13448|spider|crawl)'. If host is still
'78-236-176.adsl.cyta.gr' then of course it doesn't match, so the
condition is true.

Thats what i want.

If host doesnt contain substringsa of the regex to execute if block.
if it does match i dont want then if block to execute, hence the
counter too.

The reason i had 'cyta' before is because not mycookie or
mycookie.value != 'nikos' doesn't filter me out.

The problem is what happens with the

not mycookie or mycookie.value != 'nikos'

Since cookie 'visitor' does contain the 'nikos' value why the if code
blck gets executed?
 
M

MRAB

On 30/08/2010 02:14, Îίκος wrote:
On 29/08/2010 06:34, Îίκος wrote:
On 28/08/2010 20:37, Ãßêïò wrote:
On 16 Ãýã, 14:31, Peter Otten<[email protected]> 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)
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?

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')

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!?!!

Previously your regex was r'(cyta|yandex|13448|spider|crawl)' and you
said that host was '78-236-176.adsl.cyta.gr', so it matched.

Now your regex is r'(msn|yandex|13448|spider|crawl)'. If host is still
'78-236-176.adsl.cyta.gr' then of course it doesn't match, so the
condition is true.

Thats what i want.

If host doesnt contain substringsa of the regex to execute if block.
if it does match i dont want then if block to execute, hence the
counter too.

The reason i had 'cyta' before is because not mycookie or
mycookie.value != 'nikos' doesn't filter me out.

The problem is what happens with the

not mycookie or mycookie.value != 'nikos'

Since cookie 'visitor' does contain the 'nikos' value why the if code
blck gets executed?

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.
 
N

Nik the Greek

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?!
 
N

Nik the Greek

On 30/08/2010 03:07, Nik the Greek wrote:
On 30/08/2010 02:14, Îίκος wrote:
On 29/08/2010 06:34, Îίκος wrote:
On 28/08/2010 20:37, Ãßêïò 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!?!!
Previously your regex was r'(cyta|yandex|13448|spider|crawl)' and you
said that host was '78-236-176.adsl.cyta.gr', so it matched.
Now your regex is r'(msn|yandex|13448|spider|crawl)'. If host is still
'78-236-176.adsl.cyta.gr' then of course it doesn't match, so the
condition is true.
Thats what i want.
If host doesnt contain substringsa of the regex to execute if block.
if it does match i dont want then if block to execute, hence the
counter too.
The reason i had 'cyta' before is because not mycookie or
mycookie.value != 'nikos'  doesn't filter me out.
The problem is what happens with the
not mycookie or mycookie.value != 'nikos'
Since cookie 'visitor' does contain the 'nikos' value why the if code
blck gets executed?

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 what is the problem here?

And why mycookie when i print it it says None?

Perhpas its doenst get loaded like that?

# initialize cookie
cookie = SimpleCookie()
cookie.load( os.environ.get('HTTP_COOKIE', '') )
mycookie = cookie.get('visitor')
 
N

Nik the Greek

On 30/08/2010 03:55, Nik the Greek wrote:
On 30/08/2010 03:07, Nik the Greek wrote:
On 30/08/2010 02:14, Îίκος wrote:
On 29/08/2010 06:34, Îίκος wrote:
On 28/08/2010 20:37, Ãßêïò 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!?!!
Previously your regex was r'(cyta|yandex|13448|spider|crawl)' and you
said that host was '78-236-176.adsl.cyta.gr', so it matched.
Now your regex is r'(msn|yandex|13448|spider|crawl)'. If host is still
'78-236-176.adsl.cyta.gr' then of course it doesn't match, so the
condition is true.
Thats what i want.
If host doesnt contain substringsa of the regex to execute if block.
if it does match i dont want then if block to execute, hence the
counter too.
The reason i had 'cyta' before is because not mycookie or
mycookie.value != 'nikos'  doesn't filter me out.
The problem is what happens with the
not mycookie or mycookie.value != 'nikos'
Since cookie 'visitor' does contain the 'nikos' value why the if code
blck gets executed?
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 what is the problem here?

And why mycookie when i print it it says None?

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?
 
P

Peter Otten

Nik said:
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
 
M

MRAB

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:
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top