Getting cookie "expires" value

Brendon Wickham brendon.wickham at gmail.com
Wed Jul 22 15:28:16 CEST 2009


Hi there,

My web application uses a cookie to set/get a session id. This is
working fine. However, I'm unable to work out how to get the cookie
"expires" value.

Below is test code that replicates the problem. It creates a simple
web page and shows a cookie value if it's found, or creates a new one
(correctly setting the "expires" value) and indicates that it was
created:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/usr/bin/env python
import Cookie,os

def printHeader():
    print "Content-type: text/html\n"
    print "<html><title>Cookie test</title>"
    print "<body>"

cookies=Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE'))

if cookies.has_key('test'):
    #If cookie was found:
    printHeader()
    print "Found cookie:<br/><br/>"

    #Get cookie:
    c = cookies.get('test')

    #Print its value:
    print "<div>Value: %s</div>" % c.value

    #Iterate through its items and print name/value:
    for k,v in c.items():
         print "<div>%s=%s</div>" % (k,v)

else:
    #Define cookie:
    cookies['test'] = "1234"
    cookies['test']['expires'] = 3600

    #Save cookie:
    print cookies['test']

    #Now print HTML:
    printHeader()

    print "Cookie created."

print "</body></html>"
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


This is what I get if the cookie exists:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Found cookie:

Value: 1234
comment=
domain=
secure=
expires=
max-age=
version=
path=
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

As you can see, the cookie attributes are valueless. Can anyone help?
Am I missing something fundamental?

Cheers,

Brendon



More information about the Python-list mailing list