Cookie Problem

Jack Hatterly jackhatterly at hotmail.com
Wed Aug 10 19:39:30 EDT 2011



Hi;

I'm trying to get this cookie code to work but it doesn't set the 
properties I want set (expires, path, comment, etc.). When I delete the 
cookie and run the script it duly creates a cookie. However, when I 
reload the page none of the morsels have values associated with them. I 
also do not see values when I look for them in my FireFox CookieCuller. I
 pretty much copied this code from an online tutorial so I'm wondering 
where the problem is.



#!/usr/bin/env python



import string

import os

import datetime, Cookie, random

import time



ourTime = str(time.time())

cookie = Cookie.SimpleCookie()

cookie['lastvisit'] = str(time.time())



print cookie

print 'Content-Type: text/html\n'



print '<html><body>'

print '<p>Server time is', time.asctime(time.localtime()), '</p>'



# The returned cookie is available in the os.environ dictionary

cookie_string = os.environ.get('HTTP_COOKIE')



# The first time the page is run there will be no cookies

if not cookie_string:

    print '<p>First visit or cookies disabled</p>'

    cookie = string.replace(ourTime, '.', '')

    cookie = Cookie.SimpleCookie()

    expiration = datetime.datetime.now() + datetime.timedelta(days=30)

    cookie['lastvisit'] = str(time.time())

    cookie['lastvisit']['expires'] = 30 * 24 * 60 * 60

    cookie['lastvisit']['path'] = '/var/www/html/my_site/'

    cookie['lastvisit']['comment'] = 'holds the last user\'s visit date'

    cookie['lastvisit']['domain'] = '.www.my_site.com'

    cookie['lastvisit']['max-age'] = 30 * 24 * 60 * 60

    cookie['lastvisit']['secure'] = ''

    cookie['lastvisit']['version'] = 1



else: # Run the page twice to retrieve the cookie

   print '<p>The returned cookie string was "' + cookie_string + '"</p>'

#   cookie = string.replace(string.split(str(cookie['lastvisit']), '=')[1], '.', '')[:-1]

   # load() parses the cookie string

   cookie.load(cookie_string)

   # Use the value attribute of the cookie to get it

   lastvisit = float(cookie['lastvisit'].value)

   for morsel in cookie:

     print '<p>', morsel, '=', cookie[morsel].value

     print '<div style="margin:-1em auto auto 3em;">'

     for key in cookie[morsel]:

       print key, '=', cookie[morsel][key], '<br />'

   print '</div></p>'



print '</body></html>'



TIA,

Jack

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110810/e117cbaf/attachment.html>


More information about the Python-list mailing list