Calling Cookie Values

MRAB python at mrabarnett.plus.com
Tue Dec 15 15:05:25 EST 2009


Victor Subervi wrote:
> On Tue, Dec 15, 2009 at 2:36 PM, MRAB <python at mrabarnett.plus.com 
> <mailto:python at mrabarnett.plus.com>> wrote:
> 
>     You've just created a cookie, but are trying to get a value without
>     having set it first!
> 
> 
> LOL! Rewrote code thus:
> 
>   cookie = os.environ.get('HTTP_COOKIE')
>   if not cookie:
>     cookie = Cookie.SimpleCookie()
>     cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
>     cookie['lastvisit'] = str(time.time())
>     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
> 
> Got this error:
> 
>  /var/www/html/angrynates.com/cart/cart.py 
> <http://angrynates.com/cart/cart.py>
>   191 </html>
>   192 '''
>   193
>   194 cart()
>   195
> cart = <function cart>
>  /var/www/html/angrynates.com/cart/cart.py 
> <http://angrynates.com/cart/cart.py> in cart()
>    31   else:
>    32     cookieFlag = 'old'
>    33     print cookie['lastvisit']['expires'].value
>    34 #  Don't know what to do with this. It's for when client won't 
> accept cookies
>    35 #  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
> cookie = 'lastvisit=1260898013.65; lastvisit=1260898315.01', ].value 
> undefined
> 
> TypeError: string indices must be integers
>       args = ('string indices must be integers',)
> 
What you got from HTTP_COOKIE was a _string_ (or None). You then need to
turn it into a cookie:

     cookie_string = os.environ.get('HTTP_COOKIE')
     if cookie_string:
         cookie = Cookie.SimpleCookie(cookie_string)
         ...
     else:
         cookie = Cookie.SimpleCookie()
         ...



More information about the Python-list mailing list