Calling Cookie Values

MRAB python at mrabarnett.plus.com
Tue Dec 15 13:36:17 EST 2009


Victor Subervi wrote:
> Hi;
> 
> import Cookie
> ...
>   cookie = Cookie.SimpleCookie()
>   cookieString = os.environ.get('HTTP_COOKIE')
>   if not cookieString:
>     cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
>     cookie['lastvisit'] = str(time.time())

The following lines aren't going to work. You're setting a value to a
string and then trying to use that string as a dict...

>     cookie['lastvisit']['expires'] = cExpires
>     cookie['lastvisit']['path'] = cPath
>     cookie['lastvisit']['comment'] = cComment
>     cookie['lastvisit']['domain'] = cDomain
>     cookie['lastvisit']['max-age'] = cMaxAge
>     cookie['lastvisit']['version'] = cVersion
>     cookieFlag = 'new'
>   else:
>     cookieFlag = 'old'
>     print cookie['lastvisit']['expires'].value
> 
> Throws this error:
> 
>  /var/www/html/angrynates.com/cart/cart.py 
> <http://angrynates.com/cart/cart.py>
>   192 </html>
>   193 '''
>   194
>   195 cart()
>   196
> cart = <function cart>
>  /var/www/html/angrynates.com/cart/cart.py 
> <http://angrynates.com/cart/cart.py> in cart()
>    32   else:
>    33     cookieFlag = 'old'
>    34     print cookie['lastvisit']['expires'].value
>    35 #  Don't know what to do with this. It's for when client won't 
> accept cookies
>    36 #  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
> cookie = <SimpleCookie: >, ].value undefined
> 
> KeyError: 'lastvisit'
>       args = ('lastvisit',)
> 
> Please advise.
> 
You've just created a cookie, but are trying to get a value without
having set it first!




More information about the Python-list mailing list