Cookie Problem

Jack Hatterly jackhatterly at hotmail.com
Thu Aug 11 08:28:50 EDT 2011






> Some of these values look wrong. Your 'path' ought to be as the
> browser sees it, and your 'domain' ditto; 
...
> Otherwise, I would recommend omitting those elements and allowing the
> defaults through.

So I commented out those lines and now have this:

#!/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>'
#   for key, morsel in cookie.iteritems():
#     print "key: %s\n" % key
#     print "morsel: %s\n\n" % morsel

print '</body></html>'


and it does the same thing: namely, when I clear out the cookie, it creates a cookie but apparently not with the data I'm trying to push and when I refresh it displays all the keys without any morsel values. What do?
TIA,
Jack

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


More information about the Python-list mailing list