MemorySession 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 DbmSession is used.
This class is meant to be used as a base class for other classes that implement a session storage mechanism. req is a required reference to a mod_python request object.
BaseSession is a subclass of dict. Data can be stored and retrieved from the session by using it as a dictionary.
sid 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 sid 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 is_new() method.
Cookies generated by sessions will have a path attribute which is
calculated by comparing the server DocumentRoot
and the
directory in which the PythonHandler
directive currently in
effect was specified. E.g. if document root is /a/b/c and
PythonHandler
was specified in /a/b/c/d/e, the path
will be set to /d/e. You can force a specific path by using
ApplicationPath
option ("PythonOption ApplicationPath
/my/path" in server configuration).
When a secret is provided, BaseSession will use SignedCookie when generating cookies thereby making the session id almost impossible to fake. The default is to use plain Cookie (though even if not signed, the session id is generated to be very difficult to guess).
A session will timeout if it has not been accessed for more than timeout, which defaults to 30 minutes. An attempt to load an expired session will result in a ``new'' session.
The lock 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. lockfile is the name of a file to be used for inter-process locks.
A session is in ``new'' state when the session id was just generated, as opposed to being passed in via cookies or the sid argument.
sess = Session(req) if sess.is_new(): # redirect to login util.redirect(req, 'http://www.mysite.com/login')
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.
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.
dbm 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 mp_sess.dbm and stored in a temporary directory
returned by tempfile.gettempdir()
standard library
function. It can be overridden by setting PythonOption
SessionDbm filename
.
The implementation uses Python anydbm module, which will default to dbhash on most systems. If you need to use a specific dbm implementation (e.g. gdbm), you can pass that module as dbmtype.