Calling Cookie Values

Grant Edwards invalid at invalid.invalid
Tue Dec 15 16:01:32 EST 2009


> 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'] is a string.

>     cookie['lastvisit']['expires'] = cExpires

Here you're using the string 'expires' as an index into the
string returned by str(time.time()).  You can only index into
strings using integers.

What do you expect the following statement to do?

    '1260910829.18'['expires'] = <whatever>

>     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',)

You took the string returned by str(time.time()) and tried to
use 'expires' as an index into that time string.  You can't use
a string to index into a string.  You can only use integers.
That's what is mean't by the error message:

    TypeError: string indices must be integers

-- 
Grant Edwards                   grante             Yow! ... I have read the
                                  at               INSTRUCTIONS ...
                               visi.com            



More information about the Python-list mailing list