<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>4.7.1 Classes</title> <META NAME="description" CONTENT="4.7.1 Classes"> <META NAME="keywords" CONTENT="modpython"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <link rel="STYLESHEET" href="modpython.css"> <link rel="first" href="modpython.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="next" href="pyapi-cookie-func.html"> <LINK REL="previous" href="pyapi-cookie.html"> <LINK REL="up" href="pyapi-cookie.html"> <LINK REL="next" href="pyapi-cookie-func.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="pyapi-cookie.html"><img src="icons/previous.png" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="pyapi-cookie.html"><img src="icons/up.png" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="pyapi-cookie-func.html"><img src="icons/next.png" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Mod_python Manual</td> <td><A href="contents.html"><img src="icons/contents.png" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="icons/blank.png" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="icons/index.png" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Up:</b> <a class="sectref" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Next:</b> <a class="sectref" href="pyapi-cookie-func.html">4.7.2 Functions</A> <br><hr> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION006710000000000000000"> </A> <BR> 4.7.1 Classes </H2> <P> <dl><dt><b><span class="typelabel">class</span> <a name="l2h-182"><tt class="class">Cookie</tt></a></b>(<var>name, value</var><big>[</big><var>, attributes</var><big>]</big>) <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, __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><b><a name="l2h-183"><tt class="method">parse</tt></a></b>(<var>string</var>) <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: <dl><dd><pre class="verbatim"> mycookies = Cookie.parse("spam=eggs; expires=Sat, 14-Jun-2003 02:42:36 GMT") spamcookie = mycookies["spam"] </pre></dl> <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><b><span class="typelabel">class</span> <a name="l2h-184"><tt class="class">SignedCookie</tt></a></b>(<var>name, value, secret</var><big>[</big><var>, attributes</var><big>]</big>) <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><b><a name="l2h-185"><tt class="method">parse</tt></a></b>(<var>string, secret</var>) <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: <dl><dd><pre class="verbatim"> # 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></dl> </div> </dl> <P> </dl> <P> <dl><dt><b><span class="typelabel">class</span> <a name="l2h-186"><tt class="class">MarshalCookie</tt></a></b>(<var>name, value, secret</var><big>[</big><var>, attributes</var><big>]</big>) <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"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="pyapi-cookie.html"><img src="icons/previous.png" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="pyapi-cookie.html"><img src="icons/up.png" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="pyapi-cookie-func.html"><img src="icons/next.png" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Mod_python Manual</td> <td><A href="contents.html"><img src="icons/contents.png" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="icons/blank.png" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="icons/index.png" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Up:</b> <a class="sectref" href="pyapi-cookie.html">4.7 Cookie - HTTP</A> <b class="navlabel">Next:</b> <a class="sectref" href="pyapi-cookie-func.html">4.7.2 Functions</A> <hr> <span class="release-info">Release 3.1.0a, documentation updated on August 26, 2003.</span> </DIV> <!--End of Navigation Panel--> </BODY> </HTML>