<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>4.8.1 Classes</title> <META NAME="description" CONTENT="4.8.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="previous" href="pyapi-sess.html"> <LINK REL="up" href="pyapi-sess.html"> <LINK REL="next" href="pyapi-psp.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="pyapi-sess.html"><img src="icons/previous.png" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="pyapi-sess.html"><img src="icons/up.png" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="pyapi-psp.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-sess.html">4.8 Session - Session</A> <b class="navlabel">Up:</b> <a class="sectref" href="pyapi-sess.html">4.8 Session - Session</A> <b class="navlabel">Next:</b> <a class="sectref" href="pyapi-psp.html">4.9 _psp - Python</A> <br><hr> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION006810000000000000000"> </A> <BR> 4.8.1 Classes </H2> <P> <dl><dt><b><a name="l2h-190"><tt class="function">Session</tt></a></b>(<var>req</var><big>[</big><var>, sid, secret, timeout, lock, lockfile</var><big>]</big>) <dd> This function queries the MPM and based on that returns either a new instance of <tt class="class">DbmSession</tt> or <tt class="class">MemorySession</tt>. It takes same arguments as <tt class="class">BaseSession</tt>. <P> <tt class="class">MemorySession</tt> will be used if the MPM is threaded and not forked (such is the case on Windows), or if it threaded, forked, but only one process is allowed (the worker MPM can be configured to run this way). In all other cases <tt class="class">DbmSession</tt> is used. </dl> <P> <dl><dt><b><span class="typelabel">class</span> <a name="l2h-191"><tt class="class">BaseSession</tt></a></b>(<var>req</var><big>[</big><var>, sid, secret, timeout, lock, lockfile</var><big>]</big>) <dd> <P> This class is meant to be used as a base class for other classes that implement a session storage mechanism. <var>req</var> is a required reference to a mod_python request object. <P> <tt class="class">BaseSession</tt> is a subclass of <tt class="class">dict</tt>. Data can be stored and retrieved from the session by using it as a dictionary. <P> <var>sid</var> is an optional session id; if provided, such a session must already exist, otherwise it is ignored and a new session with a new sid is created. If <var>sid</var> is not provided, the object will attempt to look at cookies for session id. If a sid is found in cookies, but it is not previously known or the session has expired, then a new sid is created. Whether a session is ``new'' can be determined by calling the <tt class="method">is_new()</tt> method. <P> Cookies generated by sessions will have a path attribute which is calculated by comparing the server <code>DocumentRoot</code> and the directory in which the <code>PythonHandler</code> directive currently in effect was specified. E.g. if document root is <span class="file">/a/b/c</span> and <code>PythonHandler</code> was specified in <span class="file">/a/b/c/d/e</span>, the path will be set to <span class="file">/d/e</span>. You can force a specific path by using <code>ApplicationPath</code> option ("<tt class="samp">PythonOption ApplicationPath /my/path</tt>" in server configuration). <P> When a <var>secret</var> is provided, <tt class="class">BaseSession</tt> will use <tt class="class">SignedCookie</tt> when generating cookies thereby making the session id almost impossible to fake. The default is to use plain <tt class="class">Cookie</tt> (though even if not signed, the session id is generated to be very difficult to guess). <P> A session will timeout if it has not been accessed for more than <var>timeout</var>, which defaults to 30 minutes. An attempt to load an expired session will result in a ``new'' session. <P> The <var>lock</var> argument (defaults to 1) indicates whether locking should be used. When locking is on, only one session object with a particular session id can be instantiated at a time. <var>lockfile</var> is the name of a file to be used for inter-process locks. <P> A session is in ``new'' state when the session id was just generated, as opposed to being passed in via cookies or the <var>sid</var> argument. <P> <dl><dt><b><a name="l2h-192"><tt class="method">is_new</tt></a></b>() <dd> Returns 1 if this session is new. A session will also be ``new'' after an attempt to instantiate an expired or non-existent session. It is important to use this method to test whether an attempt to instantiate a session has succeeded, e.g.: <dl><dd><pre class="verbatim"> sess = Session(req) if sess.is_new(): # redirect to login util.redirect(req, 'http://www.mysite.com/login') </pre></dl> </dl> <P> <dl><dt><b><a name="l2h-193"><tt class="method">id</tt></a></b>() <dd> Returns the session id. </dl> <P> <dl><dt><b><a name="l2h-194"><tt class="method">created</tt></a></b>() <dd> Returns the session creation time in seconds since beginning of epoch. </dl> <P> <dl><dt><b><a name="l2h-195"><tt class="method">last_accessed</tt></a></b>() <dd> Returns last access time in seconds since beginning of epoch. </dl> <P> <dl><dt><b><a name="l2h-196"><tt class="method">timeout</tt></a></b>() <dd> Returns session timeout interval in seconds. </dl> <P> <dl><dt><b><a name="l2h-197"><tt class="method">set_timeout</tt></a></b>(<var>secs</var>) <dd> Set timeout to <var>secs</var>. </dl> <P> <dl><dt><b><a name="l2h-198"><tt class="method">invalidate</tt></a></b>() <dd> This method will remove the session from the persistent store and also place a header in outgoing headers to invalidate the session id cookie. </dl> <P> <dl><dt><b><a name="l2h-199"><tt class="method">load</tt></a></b>() <dd> Load the session values from storage. </dl> <P> <dl><dt><b><a name="l2h-200"><tt class="method">save</tt></a></b>() <dd> This method writes session values to storage. </dl> <P> <dl><dt><b><a name="l2h-201"><tt class="method">delete</tt></a></b>() <dd> Remove the session from storage. </dl> <P> <dl><dt><b><a name="l2h-202"><tt class="method">init_lock</tt></a></b>() <dd> This method initializes the session lock. There is no need to ever call this method, it is intended for subclasses that wish to use an alternative locking mechanism. </dl> <P> <dl><dt><b><a name="l2h-203"><tt class="method">lock</tt></a></b>() <dd> Locks this session. If the session is already locked by another thread/process, wait until that lock is released. There is no need to call this method if locking is handled automatically (default). </dl> <P> <dl><dt><b><a name="l2h-204"><tt class="method">unlock</tt></a></b>() <dd> Unlocks this session. (Same as <tt class="method">lock()</tt> - when locking is handled automatically (default), there is no need to call this method). </dl> <P> <dl><dt><b><a name="l2h-205"><tt class="method">cleanup</tt></a></b>() <dd> This method is for subclasses to implement session storage cleaning mechanism (i.e. deleting expired sessions, etc.). It will be called at random, the chance of it being called is controlled by <tt class="constant">CLEANUP_CHANCE</tt> <tt class="module">Session</tt> module variable (default 1000). This means that cleanups will be ordered at random and there is 1 in 1000 chance of it happening. Subclasses implementing this method should not perform the (potentially time consuming) cleanup operation in this method, but should instead use <tt class="method">req.register_cleanup</tt> to register a cleanup which will be executed after the request has been processed. </dl> <P> </dl> <P> <dl><dt><b><span class="typelabel">class</span> <a name="l2h-206"><tt class="class">MemorySession</tt></a></b>(<var>req, </var><big>[</big><var>, sid, secret, dbmtype, timeout</var><big>]</big>) <dd> <P> This class provides session storage using a global dictionary. This class provides by far the best performance, but cannot be used in a multi-process configuration, and also consumes memory for every active session. <P> </dl> <P> <dl><dt><b><span class="typelabel">class</span> <a name="l2h-207"><tt class="class">DbmSession</tt></a></b>(<var>req, </var><big>[</big><var>, dbm, sid, secret, dbmtype, timeout</var><big>]</big>) <dd> <P> This class provides session storage using a dbm file. Generally, dbm access is very fast, and most dbm implementations memory-map files for faster access, which makes their performance nearly as fast as direct shared memory access. <P> <var>dbm</var> is the name of the dbm file (the file must be writable by the httpd process). This file is not deleted when the server process is stopped (a nice side benefit of this is that sessions can survive server restarts). By default the session information is stored in a dbmfile named <span class="file">mp_sess.dbm</span> and stored in a temporary directory returned by <code>tempfile.gettempdir()</code> standard library function. It can be overridden by setting <code>PythonOption SessionDbm filename</code>. <P> The implementation uses Python <tt class="module">anydbm</tt> module, which will default to <tt class="module">dbhash</tt> on most systems. If you need to use a specific dbm implementation (e.g. <tt class="module">gdbm</tt>), you can pass that module as <var>dbmtype</var>. <P> </dl> <P> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="pyapi-sess.html"><img src="icons/previous.png" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="pyapi-sess.html"><img src="icons/up.png" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="pyapi-psp.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-sess.html">4.8 Session - Session</A> <b class="navlabel">Up:</b> <a class="sectref" href="pyapi-sess.html">4.8 Session - Session</A> <b class="navlabel">Next:</b> <a class="sectref" href="pyapi-psp.html">4.9 _psp - Python</A> <hr> <span class="release-info">Release 3.1.0a, documentation updated on August 26, 2003.</span> </DIV> <!--End of Navigation Panel--> </BODY> </HTML>