<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <link rel="STYLESHEET" href="modpython.css" type='text/css' /> <link rel="first" href="modpython.html" title='Mod_python Manual' /> <link rel='contents' href='contents.html' title="Contents" /> <link rel='index' href='genindex.html' title='Index' /> <link rel='last' href='about.html' title='About this document...' /> <link rel='help' href='about.html' title='About this document...' /> <link rel="next" href="pyapi-cookie-func.html" /> <link rel="prev" href="pyapi-cookie.html" /> <link rel="parent" href="pyapi-cookie.html" /> <link rel="next" href="pyapi-cookie-func.html" /> <meta name='aesop' content='information' /> <title>4.7.1 Classes</title> </head> <body> <DIV CLASS="navigation"> <div id='top-navigation-panel' xml:id='top-navigation-panel'> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="4.7 Cookie - HTTP" href="pyapi-cookie.html"><img src='previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="4.7 Cookie - HTTP" href="pyapi-cookie.html"><img src='up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="4.7.2 Functions" href="pyapi-cookie-func.html"><img src='next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Mod_python Manual</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="contents.html"><img src='contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><img src='blank.png' border='0' height='32' alt='' width='32' /></td> <td class='online-navigation'><a rel="index" title="Index" href="genindex.html"><img src='index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="pyapi-cookie-func.html">4.7.2 Functions</A> </div> <hr /></div> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION006710000000000000000"></A><A NAME="pyapi-cookie-classes"></A> <BR> 4.7.1 Classes </H2> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-229' xml:id='l2h-229' class="class">Cookie</tt></b>(</nobr></td> <td><var>name, value</var><big>[</big><var>, attributes</var><big>]</big><var></var>)</td></tr></table></dt> <dd> <P> This class is used to construct a single cookie named <var>name</var> and having <var>value</var> as the value. Additionally, any of the attributes defined in the Netscape specification and RFC2109 can by supplied as keyword arguments. <P> The attributes of the class represent cookie attributes, and their string representations become part of the string representation of the cookie. The <tt class="class">Cookie</tt> class restricts attribute names to only valid values, specifically, only the following attributes are allowed: <code>name, value, version, path, domain, secure, comment, expires, max_age, commentURL, discard, port, httponly, __data__</code>. <P> The <code>__data__</code> attribute is a general-purpose dictionary that can be used for storing arbitrary values, when necessary (This is useful when subclassing <tt class="class">Cookie</tt>). <P> The <tt class="member">expires</tt> attribute is a property whose value is checked upon setting to be in format "<tt class="samp">Wdy, DD-Mon-YYYY HH:MM:SS GMT</tt>" (as dictated per Netscape cookie specification), or a numeric value representing time in seconds since beginning of epoch (which will be automatically correctly converted to GMT time string). An invalid <code>expires</code> value will raise <tt class="exception">ValueError</tt>. <P> When converted to a string, a <tt class="class">Cookie</tt> will be in correct format usable as value in a "<tt class="samp">Cookie</tt>" or "<tt class="samp">Set-Cookie</tt>" header. <P> <div class="note"><b class="label">Note:</b> Unlike the Python Standard Library Cookie classes, this class represents a single cookie (referred to as <i class="dfn">Morsel</i> in Python Standard Library). </div> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-230' xml:id='l2h-230' class="method">parse</tt></b>(</nobr></td> <td><var>string</var>)</td></tr></table></dt> <dd> This is a class method that can be used to create a <tt class="class">Cookie</tt> instance from a cookie string <var>string</var> as passed in a header value. During parsing, attribute names are converted to lower case. <P> Because this is a class method, it must be called explicitly specifying the class. <P> This method returns a dictionary of <tt class="class">Cookie</tt> instances, not a single <tt class="class">Cookie</tt> instance. <P> Here is an example of getting a single <tt class="class">Cookie</tt> instance: <div class="verbatim"><pre> mycookies = Cookie.parse("spam=eggs; expires=Sat, 14-Jun-2003 02:42:36 GMT") spamcookie = mycookies["spam"] </pre></div> <P> <div class="note"><b class="label">Note:</b> Because this method uses a dictionary, it is not possible to have duplicate cookies. If you would like to have more than one value in a single cookie, consider using a <tt class="class">MarshalCookie</tt>. </div> <P> </dl> <P> </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-231' xml:id='l2h-231' class="class">SignedCookie</tt></b>(</nobr></td> <td><var>name, value, secret</var><big>[</big><var>, attributes</var><big>]</big><var></var>)</td></tr></table></dt> <dd> <P> This is a subclass of <tt class="class">Cookie</tt>. This class creates cookies whose name and value are automatically signed using HMAC (md5) with a provided secret <var>secret</var>, which must be a non-empty string. <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-232' xml:id='l2h-232' class="method">parse</tt></b>(</nobr></td> <td><var>string, secret</var>)</td></tr></table></dt> <dd> This method acts the same way as <tt class="class">Cookie.parse()</tt>, but also verifies that the cookie is correctly signed. If the signature cannot be verified, the object returned will be of class <tt class="class">Cookie</tt>. <P> <div class="note"><b class="label">Note:</b> Always check the types of objects returned by <tt class="method">SignedCookie.parse()</tt>. If it is an instance of <tt class="class">Cookie</tt> (as opposed to <tt class="class">SignedCookie</tt>), the signature verification has failed: <div class="verbatim"><pre> # assume spam is supposed to be a signed cookie if type(spam) is not Cookie.SignedCookie: # do something that indicates cookie isn't signed correctly </pre></div> </div> </dl> <P> </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-233' xml:id='l2h-233' class="class">MarshalCookie</tt></b>(</nobr></td> <td><var>name, value, secret</var><big>[</big><var>, attributes</var><big>]</big><var></var>)</td></tr></table></dt> <dd> <P> This is a subclass of <tt class="class">SignedCookie</tt>. It allows for <var>value</var> to be any marshallable objects. Core Python types such as string, integer, list, etc. are all marshallable object. For a complete list see <em class="citetitle"><a href="http://www.python.org/doc/current/lib/module-marshal.html" title="marshal" >marshal</a></em> module documentation. <P> When parsing, the signature is checked first, so incorrectly signed cookies will not be unmarshalled. <P> </dl> <P> <DIV CLASS="navigation"> <div class='online-navigation'> <p></p><hr /> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="4.7 Cookie - HTTP" href="pyapi-cookie.html"><img src='previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="4.7 Cookie - HTTP" href="pyapi-cookie.html"><img src='up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="4.7.2 Functions" href="pyapi-cookie-func.html"><img src='next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Mod_python Manual</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="contents.html"><img src='contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><img src='blank.png' border='0' height='32' alt='' width='32' /></td> <td class='online-navigation'><a rel="index" title="Index" href="genindex.html"><img src='index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="pyapi-cookie-func.html">4.7.2 Functions</A> </div> </div> <hr /> <span class="release-info">Release 3.3.1, documentation updated on January 29, 2007.</span> </DIV> <!--End of Navigation Panel--> </BODY> </HTML>