Cookie Class

The Cookie class is the class from which the other cookie classes derives. It constructs a basic cookie object. Beyond the name and value the cookie object can have any of these attributes: version, path, domain, secure, comment, expires, max_age, commentURL, discard, port, __data__

After creating a cookie object with the Cookie class the add_cookie() function is used to set a cookie in the HTTP header.

This will set a cookie named last_visit:

from mod_python import Cookie
import time

def index(req):

   # Create a cookie object named last_visit
   # with the value of the server's current UTC time
   c = Cookie.Cookie('last_visit', time.time())

   # The cookie will expire in 30 days.
   c.expires = time.time() + 30 * 24 * 60 * 60

   # Add the cookie to the HTTP header.
   Cookie.add_cookie(req, c)

   return """\
<html><body>
<p>%s</p>
<p>%s</p>
</body></html>
""" % ('You have just received this cookie:', c)