MINI SHELL

Server : Apache/2.2.2 (Fedora)
System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686
User : apache ( 48)
PHP Version : 5.2.9
Disable Function : NONE
Directory :  /var/www/manual/mod/mod_python/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/manual/mod/mod_python/pyapi-sess-classes.html
<!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-sess-example.html">
<LINK REL="prev" href="pyapi-sess.html">
<LINK REL="parent" href="pyapi-sess.html">
<LINK REL="next" href="pyapi-sess-example.html">
<meta name='aesop' content='information'>
<META NAME="description" CONTENT="Classes">
<META NAME="keywords" CONTENT="modpython">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<title>4.8.1 Classes</title>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><a rel="prev" title="4.8 Session - Session" 
  href="pyapi-sess.html"><img src='previous.gif'
  border='0' height='32'  alt='Previous Page' width='32'></A></td>
<td><a rel="parent" title="4.8 Session - Session" 
  href="pyapi-sess.html"><img src='up.gif'
  border='0' height='32'  alt='Up One Level' width='32'></A></td>
<td><a rel="next" title="4.8.2 Examples" 
  href="pyapi-sess-example.html"><img src='next.gif'
  border='0' height='32'  alt='Next Page' width='32'></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td><a rel="contents" title="Table of Contents" 
  href="contents.html"><img src='contents.gif'
  border='0' height='32'  alt='Contents' width='32'></A></td>
<td><img src='blank.gif'
  border='0' height='32'  alt='' width='32'></td>
<td><a rel="index" title="Index" 
  href="genindex.html"><img src='index.gif'
  border='0' height='32'  alt='Index' width='32'></A></td>
</tr></table>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="pyapi-sess.html">4.8 Session - Session</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="pyapi-sess.html">4.8 Session - Session</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pyapi-sess-example.html">4.8.2 Examples</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION006810000000000000000">&nbsp;</A>
<BR>
4.8.1 Classes
</H2>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-208"><tt class="function">Session</tt></a></b>(</nobr></td>
  <td><var>req</var><big>[</big><var>, sid, secret, timeout, lock</var><big>]</big>)</td></tr></table>
<dd>

<P>
<tt class="function">Session()</tt> takes the same arguments as <tt class="class">BaseSession</tt>.

<P>
This function returns a instance of the default session class. The
  the session class to be used can be specified using 
  <var>PythonOption session value</var>, where <var>value</var> is one of
  <tt class="class">DbmSession</tt>, <tt class="class">MemorySession</tt> or <tt class="class">FileSession</tt>. Specifying
  custom session classes using PythonOption session is not yet supported.

<P>
If <var>PythonOption session</var> is not found, the 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>.

<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<a name="l2h-209"><tt class="class">BaseSession</tt></a></b>(</nobr></td>
  <td><var>req</var><big>[</big><var>, sid, secret, timeout, lock</var><big>]</big>)</td></tr></table>
<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.

<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-210"><tt class="method">is_new</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<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.:
    <div class="verbatim"><pre>
sess = Session(req)
if sess.is_new():
    # redirect to login
    util.redirect(req, 'http://www.mysite.com/login')
</pre></div>
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-211"><tt class="method">id</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    Returns the session id.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-212"><tt class="method">created</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    Returns the session creation time in seconds since beginning of
    epoch.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-213"><tt class="method">last_accessed</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    Returns last access time in seconds since beginning of epoch.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-214"><tt class="method">timeout</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    Returns session timeout interval in seconds.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-215"><tt class="method">set_timeout</tt></a></b>(</nobr></td>
  <td><var>secs</var>)</td></tr></table>
<dd>
    Set timeout to <var>secs</var>.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-216"><tt class="method">invalidate</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-217"><tt class="method">load</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    Load the session values from storage.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-218"><tt class="method">save</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    This method writes session values to storage.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-219"><tt class="method">delete</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<dd>
    Remove the session from storage.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-220"><tt class="method">init_lock</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-221"><tt class="method">lock</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<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).

<P>
This method registeres a cleanup which always unlocks the session
    at the end of the request processing.
  </dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-222"><tt class="method">unlock</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><a name="l2h-223"><tt class="method">cleanup</tt></a></b>(</nobr></td>
  <td>)</td></tr></table>
<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<a name="l2h-224"><tt class="class">DbmSession</tt></a></b>(</nobr></td>
  <td><var>req, </var><big>[</big><var>, dbm, sid, secret, dbmtype, timeout, lock</var><big>]</big>)</td></tr></table>
<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. An alternative directory can be specified with the  
  <code>PythonOption session_directory</code> directive.  
  The path and filename can can be overridden by setting <code>PythonOption
  session_dbm 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>
Note that using this class directly is not cross-platform. For best
  compatibility across platforms, always use the <tt class="function">Session()</tt>
  function to create sessions.

<P>
</dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<a name="l2h-225"><tt class="class">FileSession</tt></a></b>(</nobr></td>
  <td><var>req, </var><big>[</big><var>, sid, secret, timeout, lock, fast_cleanup, verify_cleanup</var><big>]</big>)</td></tr></table>
<dd>

<P>
New in version 3.2.0.

<P>
This class provides session storage using a separate file for each
  session. It is a subclass of <tt class="module">BaseSession</tt>.

<P>
Session data is stored in a separate file for each session. These 
  files are not deleted when the server process is stopped, so
  sessions are persistent across server restarts.  
  The session files are saved in a directory named mp_sess in the 
  temporary directory returned by the <code>tempfile.gettempdir()</code> 
  standard library function. An alternate path can be set using 
  <code>PythonOption session_directory /path/to/directory</code>. This
  directory must exist and be readable and writeable by the apache
  process.

<P>
Expired session files are periodically removed by the cleanup
  mechanism. The behaviour of the cleanup can be controlled using the 
  <var>fast_cleanup</var> and <var>verify_cleanup</var> parameters, as well as 
  <var>PythonOption session_grace_period</var> and
  <var>PythonOption session_cleanup_time_limit</var>.

<P>

<UL>
<LI><var>fast_cleanup</var>
    A boolean value used to turn on FileSession cleanup optimization.
    Default is <var>True</var> and will result in reduced cleanup time when
    there are a large number of session files.

<P>
When <var>fast_cleanup</var> is True, the modification time for the session
    file is used to determine if it is a candidate for deletion.
    If <code>(current_time - file_modification_time) &gt; (timeout + grace_period)</code>,
    the file will be a candidate for deletion. If <var>verify_cleanup</var>
    is False, no futher checks will be made and the file will be
    deleted.

<P>
If <var>fast_cleanup</var> is False, the session file will unpickled and
    it's timeout value used to determine if the session is a candidate for
    deletion. <var>fast_cleanup</var> = False implies <var>verify_cleanup</var> =
    True.

<P>
The timeout used in the fast_cleanup calculation is same as the
    timeout for the session in the current request running the
    filesession_cleanup. If your session objects are not using the same
    timeout, or you are manually setting the timeout for a particular
    session with <code>set_timeout()</code>, you will need to set 
    <var>verify_cleanup</var> = True.

<P>
The value of <var>fast_cleanup</var> can also be set using
    <code>PythonOption session_fast_cleanup</code>.

<P>
</LI>
<LI><var>verify_cleanup</var>
    Boolean value used to optimize the FileSession cleanup process.
    Default is <code>True</code>.

<P>
If <var>verify_cleanup</var> is True, the session file which is being 
    considered for deletion will be unpickled and its timeout value
    will be used to decide if the file should be deleted. 

<P>
When <var>verify_cleanup</var> is False, the timeout value for the current
    session will be used in to determine if the session has expired. In
    this case, the session data will not be read from disk, which can
    lead to a substantial performance improvement when there are a large
    number of session files, or where each session is saving a large 
    amount of data. However this may result in valid sessions being
    deleted if all the sessions are not using a the same timeout value.

<P>
The value of <var>verify_cleanup</var> can also be set using
    <code>PythonOption session_verify_cleanup</code>

<P>
</LI>
<LI><var>PythonOption session_cleanup_time_limit [value]</var>
    Integer value in seconds. Default is 2 seconds.

<P>
Session cleanup could potentially take a long time and be both cpu
    and disk intensive, depending on the number of session files and if
    each file needs to be read to verify the timeout value. To avoid
    overloading the server, each time filesession_cleanup is called it
    will run for a maximum of <var>session_cleanup_time_limit</var> seconds.
    Each cleanup call will resume from where the previous call left off
    so all session files will eventually be checked.

<P>
Setting <var>session_cleanup_time_limit</var> to 0 will disable this 
    feature and filesession_cleanup will run to completion each time it
    is called.

<P>
</LI>
<LI><var>PythonOption session_grace_period [value]</var>
    Integer value in seconds. Default is 240 seconds. This value is added
    to the session timeout in determining if a session file should be 
    deleted.

<P>
There is a small chance that a the cleanup for a given session file
    may occur at the exact time that the session is being accessed by
    another request. It is possible under certain circumstances for that
    session file to be saved in the other request only to be immediately 
    deleted by the cleanup. To avoid this race condition, a session is
    allowed a <var>grace_period</var> before it is considered for deletion by
    the cleanup.  As long as the grace_period is longer that the time it
    takes to complete the request (which should normally be less than 1
    second), the session will not be mistakenly deleted by the cleanup.

<P>
The default value should be sufficient for most applications.
  
</LI>
</UL>

<P>
</dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<a name="l2h-226"><tt class="class">MemorySession</tt></a></b>(</nobr></td>
  <td><var>req, </var><big>[</big><var>, sid, secret, timeout, lock</var><big>]</big>)</td></tr></table>
<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>
Note that using this class directly is not cross-platform. For best
  compatibility across platforms, always use the <tt class="function">Session()</tt>
  function to create sessions.

<P>
</dl>

<P>

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><a rel="prev" title="4.8 Session - Session" 
  rel="prev" title="4.8 Session - Session" 
  href="pyapi-sess.html"><img src='previous.gif'
  border='0' height='32'  alt='Previous Page' width='32'></A></td>
<td><a rel="parent" title="4.8 Session - Session" 
  rel="parent" title="4.8 Session - Session" 
  href="pyapi-sess.html"><img src='up.gif'
  border='0' height='32'  alt='Up One Level' width='32'></A></td>
<td><a rel="next" title="4.8.2 Examples" 
  rel="next" title="4.8.2 Examples" 
  href="pyapi-sess-example.html"><img src='next.gif'
  border='0' height='32'  alt='Next Page' width='32'></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td><a rel="contents" title="Table of Contents" 
  rel="contents" title="Table of Contents" 
  href="contents.html"><img src='contents.gif'
  border='0' height='32'  alt='Contents' width='32'></A></td>
<td><img src='blank.gif'
  border='0' height='32'  alt='' width='32'></td>
<td><a rel="index" title="Index" 
  rel="index" title="Index" 
  href="genindex.html"><img src='index.gif'
  border='0' height='32'  alt='Index' width='32'></A></td>
</tr></table>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="pyapi-sess.html">4.8 Session - Session</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="pyapi-sess.html">4.8 Session - Session</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pyapi-sess-example.html">4.8.2 Examples</A>
<hr>
<span class="release-info">Release 3.2.8, documentation updated on February 19, 2006.</span>
</DIV>
<!--End of Navigation Panel-->

</BODY>
</HTML>

Anon7 - 2021