Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trying to set a cookie within a python script

Reply
Thread Tools

Trying to set a cookie within a python script

 
 
Νίκος
Guest
Posts: n/a
 
      08-01-2010
Hello fellow Python Coders!

I don't see the error in my attempt to set a a cookie in this test
script. Can you help please?! Thank you!

Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import Cookie

print ( "Content-type: text/html\n" )

cookie = Cookie.SimpleCookie()

if cookie.has_key('visitor') == "nikos"      #if visitor cookie exist
   print ( "Δεν σε ειδα, δεν σε ξερω, δεν σε ακουσα. Θα εισαι ο
αοραστος επισκεπτης!!" )
   cookie['visitor'] = ( 'nikos', time() - 1 )      #this cookie will
expire now
else
   print ( "Απο δω και στο εξης δεν θα αυξανω τον μετρητη δικη σου
επισκεψη!!" )
   cookie['visitor'] = ( 'nikos', time() + 60*60*24*365 )      #this
cookie will expire in an year
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-02-2010
On Sun, 01 Aug 2010 09:56:48 -0700, Νίκος wrote:

> Hello fellow Python Coders!
>
> I don't see the error in my attempt to set a a cookie in this test
> script.


Neither do I. What makes you think there is an error? What sort of error?
Do you get a core dump, an exception, or something else?

Please report what you get, and what you expect, and how they are
different.


--
Steven
 
Reply With Quote
 
 
 
 
Νίκος
Guest
Posts: n/a
 
      08-02-2010
>On 2 Αύγ, 03:52, Steven D'Aprano <steve-REMOVE-T...@cybersource.com.au> wrote:

> Neither do I. What makes you think there is an error? What sort of error?
> Do you get a core dump, an exception, or something else?
>
> Please report what you get, and what you expect, and how they are
> different.


Hello Steven,

Here is the script when it tries to run from my remote web server:
http://www.webville.gr/cgi-bin/koukos.py

Its seems the error is in this line of code, somwthing with time.

19 cookie['visitor'] = ( 'nikos', time() +
60*60*24*365 ) #this cookie will expire in an year

 
Reply With Quote
 
Νίκος
Guest
Posts: n/a
 
      08-02-2010
Also my greek print appear in funny encoding although i do use # -*-
coding: utf-8 -*-
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      08-02-2010
Νίκος wrote:
> Also my greek print appear in funny encoding although i do use # -*-
> coding: utf-8 -*-


That line just tells Python what encoding the source file itself uses.
It doesn't affect what the program does or how it runs.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-02-2010
On Sun, 01 Aug 2010 19:16:29 -0700, Νίκος wrote:

>>On 2 Αύγ, 03:52, Steven D'Aprano <steve-REMOVE-T...@cybersource.com.au>
>>wrote:

>
>> Neither do I. What makes you think there is an error? What sort of
>> error? Do you get a core dump, an exception, or something else?
>>
>> Please report what you get, and what you expect, and how they are
>> different.

>
> Hello Steven,
>
> Here is the script when it tries to run from my remote web server:
> http://www.webville.gr/cgi-bin/koukos.py
>
> Its seems the error is in this line of code, somwthing with time.
>
> 19 cookie['visitor'] = ( 'nikos', time() + 60*60*24*365 )
> #this cookie will expire in an year



What error? Please copy and paste (do not retype) the entire error you
get.


--
Steven

 
Reply With Quote
 
Νίκος
Guest
Posts: n/a
 
      08-02-2010
If you just click in my web page to see the script run in action due
to the cgitb module i use it will provide you both the source code
that the error appears and the error as well.

All you have to do is click here: http://www.webville.gr/cgi-bin/koukos.py

As for the encoding why when i print greek characters they dont appear
correctly in chrome in runtime?
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-02-2010
On Sun, 01 Aug 2010 23:39:34 -0700, Νίκος wrote:

> If you just click in my web page to see the script run in action due to
> the cgitb module i use it will provide you both the source code that the
> error appears and the error as well.
>
> All you have to do is click here:
> http://www.webville.gr/cgi-bin/koukos.py


I'll do this just once, but next time, don't expect others to track down
the error message for you. We're volunteers, we don't owe you anything,
so if you want us to help, you make it easy for us. Some people have
access to email, but not web. If you can't be bothered to copy and paste
the error message into an email or news post, why should we be bothered
to help you?


The error you are getting is:

NameError: name 'time' is not defined

That tells you that you don't have a function called time() defined
anywhere. You need to import the time module first:

import time

and then use the fully qualified function name time.time(), or do:

from time import time

and then use the function alone time().

> As for the encoding why when i print greek characters they dont appear
> correctly in chrome in runtime?


What encoding does the web page claim to be?

You need to check the document encoding, and see that it matches the
document encoding you are actually using.




--
Steven
 
Reply With Quote
 
Νίκος
Guest
Posts: n/a
 
      08-02-2010
Steven,

First of all thank you for your response. I cant beleive i neglected
to import the time module!

The only reason that i asked you guys to follow the link was for you
to see the actualt coding and error report as python produces it by
itself with all the relative characteristics. Of course it was not due
to boredom and there was no need to be aggresive with me as this
wasn't the case. I thouigh that by giving the URL was easier for you
guys.

Now the script runs but for some reason only the code block within the
'else' tun each time:

This:
else:
print "Ξ‘Ξ*Ο ΔΩ ΞšΞ‘Ξ™ Σ΀Ο Ξ•ΞžΞ—Ξ£ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞžΞ•Ξ‘Ξ©, ΔΕΝ ΣΕ Ξ‘ΞšΞŸΞ₯ΣΑ!
Ξ˜Ξ‘ ΕΙΣΑΙ Ξ*Ξ›Ξ•ΞŸΞ Ο Ξ‘ΞŸΞ‘Ξ‘Ξ€ΞŸΞ£ Ξ•Ξ*Ξ™Ξ£ΞšΞ•Ξ*΀ΗΣ!!"
cookie['visitor'] = ( 'nikos', time() + 60*60*24*365 ) #this cookie
will expire in an year

The cookie is only get set and never expires

i changed the if with this

if os.environ.get('HTTP_COOKIE') and cookie.has_key('visitor') ==
'nikos': #if visitor cookie exist

but still no luck.

As for the encoding Notepad++, which is what i use for an editor say
its UTF-8 without BOM.

Isn't this what i'm supposed to use?

My Python scripts only containes english and greek letters, so i
though usign UTF-8 is the way to go. No?! Please if you explain to me
in greater detail!
 
Reply With Quote
 
Νίκος
Guest
Posts: n/a
 
      08-02-2010
Hello, any ideas?!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Cookie Script: Not recognising Cookie Jimbo Python 1 04-08-2010 07:34 AM
How to set a cookie using Cookie Module tryg.olson@gmail.com Python 2 01-08-2009 02:39 PM
Is there a way to set a cookie with CGI::Cookie objects, from anrhtml file? Xeno Campanoli Ruby 2 07-09-2008 11:07 PM
Cookie Confusion - How to Set a Cookie cbhoem@gmail.com Python 5 05-02-2008 02:56 PM
Cookies set one time, I delete cookie, cookie is never set again! Phil Powell Javascript 2 09-05-2003 02:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57