Problem reading HTTP_COOKIE

Jack Hatterly jackhatterly at hotmail.com
Sat Aug 13 00:53:16 CEST 2011


Chris, I finally got a script that works. Thanks for trying!
Jack

#!/usr/bin/env python

import string
import os
import datetime, Cookie, random
import time

# The returned cookie is available in the os.environ dictionary
cookie_string = os.environ.get('HTTP_COOKIE')

if not cookie_string:
  ourTime = str(time.time())
  cookie = Cookie.SimpleCookie()
  cookie['lastvisit'] = string.replace(ourTime, '.', '')
  print cookie
  print 'Content-Type: text/html\n'
  print '<html><body>'
  print '<p>Server time is', time.asctime(time.localtime()), '</p>'
  # The first time the page is run there will be no cookies
  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'] = string.replace(ourTime, '.', '')
else: # Run the page twice to retrieve the cookie
  cookie = Cookie.SimpleCookie()
  cookie['lastvisit'] = cookie_string
  cookie['lastvisit']['expires'] = 30 * 24 * 60 * 60
  cookie['lastvisit']['path'] = '/cgi-bin/'
  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
  print 'Content-Type: text/html\n'
  print '<p>', cookie, '</p>'
  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>'


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


More information about the Python-list mailing list