[code.view]

[top] / python / PyMOTW / Cookie / Cookie_Morsel.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import Cookie
     import datetime
     
     def show_cookie(c):
         print c
         for key, morsel in c.iteritems():
             print
             print 'key =', morsel.key
             print '  value =', morsel.value
             print '  coded_value =', morsel.coded_value
             for name in morsel.keys():
                 if morsel[name]:
                     print '  %s = %s' % (name, morsel[name])
     
     c = Cookie.SimpleCookie()
     
     # A cookie with a value that has to be encoded to fit into the header
     c['encoded_value_cookie'] = '"cookie_value"'
     c['encoded_value_cookie']['comment'] = 'Notice that this cookie value has escaped quotes'
     
     # A cookie that only applies to part of a site
     c['restricted_cookie'] = 'cookie_value'
     c['restricted_cookie']['path'] = '/sub/path'
     c['restricted_cookie']['domain'] = 'PyMOTW'
     c['restricted_cookie']['secure'] = True
     
     # A cookie that expires in 5 minutes
     c['with_max_age'] = 'expires in 5 minutes'
     c['with_max_age']['max-age'] = 300 # seconds
     
     # A cookie that expires at a specific time
     c['expires_at_time'] = 'cookie_value'
     expires = datetime.datetime(2009, 2, 14, 18, 30, 14) + datetime.timedelta(hours=1)
     c['expires_at_time']['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S') # Wdy, DD-Mon-YY HH:MM:SS GMT
     
     show_cookie(c)

[top] / python / PyMOTW / Cookie / Cookie_Morsel.py

contact | logmethods.com