Cookie Problem
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 '' print 'Server time is', time.asctime(time.localtime()), '' # 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 'First visit or cookies disabled' 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 'The returned cookie string was "' + cookie_string + '"' # 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 '', morsel, '=', cookie[morsel].value print '' for key in cookie[morsel]: print key, '=', cookie[morsel][key], '' print '' print '' TIA, Jack -- http://mail.python.org/mailman/listinfo/python-list
RE: Cookie Problem
> 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 '' print 'Server time is', time.asctime(time.localtime()), '' # 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 'First visit or cookies disabled' 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 'The returned cookie string was "' + cookie_string + '"' # 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 '', morsel, '=', cookie[morsel].value print '' for key in cookie[morsel]: print key, '=', cookie[morsel][key], '' print '' # for key, morsel in cookie.iteritems(): # print "key: %s\n" % key # print "morsel: %s\n\n" % morsel print '' 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 -- http://mail.python.org/mailman/listinfo/python-list
Problem reading HTTP_COOKIE
Hi; I'm trying to get cookies to work and I've traced my problem down to this reduced script: #! /usr/bin/python import string import os import datetime, Cookie, random import time import os def parse_cookie(): print 'Content-Type: text/html\n' print os.environ.get('HTTP_COOKIE') print '' print '' if __name__ == "__main__": parse_cookie() It prints "None". However, when I look in my browser's cookie jar, there is a cookie "www.my_site.com" where my_site is the site from which I am surfing the above script. What gives? TIA, Jack -- http://mail.python.org/mailman/listinfo/python-list
RE: Problem reading HTTP_COOKIE
> Date: Fri, 12 Aug 2011 21:29:25 +0100 > Subject: Re: Problem reading HTTP_COOKIE > From: ros...@gmail.com > To: python-list@python.org > > I assume that what you mean is that it prints "None" even after you > hit F5 to reload the page, as the first run of the script will never > have a cookie. > > Your script is now not actually setting any cookie. What cookie are > you seeing in your browser? It may be something created and consumed > by your framework, somewhere. But my script *is* setting a cookie. I just sent a stripped-down version of the script earlier. Here's the entire script: #! /usr/bin/python import string import os import datetime, Cookie, random import time import os def parse_cookie(): ourTime = str(time.time()) environ = os.environ cookie = environ.get('HTTP_COOKIE', '') if cookie == '': cookie = string.replace(ourTime, '.', '') cookie = Cookie.SimpleCookie() expiration = datetime.datetime.now() + datetime.timedelta(days=30) cookie['lastvisit'] = random.randint(1,) cookie['lastvisit']['expires'] = 30 * 24 * 60 * 60 cookie['lastvisit']['path'] = '/var/www/html/my_site/clients/Millenium/' cookie['lastvisit']['comment'] = random.randint(1,) cookie['lastvisit']['domain'] = '.www.my_site.com' cookie['lastvisit']['max-age'] = 30 * 24 * 60 * 60 cookie['lastvisit']['secure'] = '' cookie['lastvisit']['version'] = 1 c = Cookie.SimpleCookie() c.load(cookie) cookiedict = {} for key in c.keys(): cookiedict[key] = c.get(key).value print c print 'Content-Type: text/html\n' print '' print '' if __name__ == "__main__": parse_cookie() You see, every time when the cookie is read it comes back None which gets translated to '' and then the cookie is re-baked. *NOT* what I want!! I can see the cookie in my browser. TIA, Jack -- http://mail.python.org/mailman/listinfo/python-list
RE: Problem reading HTTP_COOKIE
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 '' print 'Server time is', time.asctime(time.localtime()), '' # The first time the page is run there will be no cookies print 'First visit or cookies disabled' 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 '', cookie, '' for morsel in cookie: print '', morsel, '=', cookie[morsel].value print '' for key in cookie[morsel]: print key, '=', cookie[morsel][key], '' print '' -- http://mail.python.org/mailman/listinfo/python-list