/usr/share/doc/python-werkzeug-doc/html/contrib/sessions.html is in python-werkzeug-doc 0.9.6+dfsg-1+deb8u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sessions — Werkzeug 0.9.6 documentation</title>
<link rel="stylesheet" href="../_static/werkzeug.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.9.6',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="Werkzeug 0.9.6 documentation" href="../index.html" />
<link rel="up" title="Contributed Modules" href="index.html" />
<link rel="next" title="Secure Cookie" href="securecookie.html" />
<link rel="prev" title="Atom Syndication" href="atom.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="securecookie.html" title="Secure Cookie"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="atom.html" title="Atom Syndication"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">Werkzeug 0.9.6 documentation</a> »</li>
<li><a href="index.html" accesskey="U">Contributed Modules</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-werkzeug.contrib.sessions">
<span id="sessions"></span><h1>Sessions<a class="headerlink" href="#module-werkzeug.contrib.sessions" title="Permalink to this headline">¶</a></h1>
<p>This module contains some helper classes that help one to add session
support to a python WSGI application. For full client-side session
storage see <a class="reference internal" href="securecookie.html#module-werkzeug.contrib.securecookie" title="werkzeug.contrib.securecookie"><tt class="xref py py-mod docutils literal"><span class="pre">securecookie</span></tt></a> which implements a
secure, client-side session storage.</p>
<div class="section" id="application-integration">
<h2>Application Integration<a class="headerlink" href="#application-integration" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.contrib.sessions</span> <span class="kn">import</span> <span class="n">SessionMiddleware</span><span class="p">,</span> \
<span class="n">FilesystemSessionStore</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">SessionMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">FilesystemSessionStore</span><span class="p">())</span>
</pre></div>
</div>
<p>The current session will then appear in the WSGI environment as
<cite>werkzeug.session</cite>. However it’s recommended to not use the middleware
but the stores directly in the application. However for very simple
scripts a middleware for sessions could be sufficient.</p>
<p>This module does not implement methods or ways to check if a session is
expired. That should be done by a cronjob and storage specific. For
example to prune unused filesystem sessions one could check the modified
time of the files. It sessions are stored in the database the new()
method should add an expiration timestamp for the session.</p>
<p>For better flexibility it’s recommended to not use the middleware but the
store and session object directly in the application dispatching:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">session_store</span> <span class="o">=</span> <span class="n">FilesystemSessionStore</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
<span class="n">sid</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">cookies</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'cookie_name'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">sid</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">request</span><span class="o">.</span><span class="n">session</span> <span class="o">=</span> <span class="n">session_store</span><span class="o">.</span><span class="n">new</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">request</span><span class="o">.</span><span class="n">session</span> <span class="o">=</span> <span class="n">session_store</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">get_the_response_object</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
<span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">should_save</span><span class="p">:</span>
<span class="n">session_store</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">)</span>
<span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="s">'cookie_name'</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">sid</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="reference">
<h2>Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.contrib.sessions.Session">
<em class="property">class </em><tt class="descclassname">werkzeug.contrib.sessions.</tt><tt class="descname">Session</tt><big>(</big><em>data</em>, <em>sid</em>, <em>new=False</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.Session" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclass of a dict that keeps track of direct object changes. Changes
in mutable structures are not tracked, for those you have to set
<cite>modified</cite> to <cite>True</cite> by hand.</p>
<dl class="attribute">
<dt id="werkzeug.contrib.sessions.Session.sid">
<tt class="descname">sid</tt><a class="headerlink" href="#werkzeug.contrib.sessions.Session.sid" title="Permalink to this definition">¶</a></dt>
<dd><p>The session ID as string.</p>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.contrib.sessions.Session.new">
<tt class="descname">new</tt><a class="headerlink" href="#werkzeug.contrib.sessions.Session.new" title="Permalink to this definition">¶</a></dt>
<dd><p><cite>True</cite> is the cookie was newly created, otherwise <cite>False</cite></p>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.contrib.sessions.Session.modified">
<tt class="descname">modified</tt><a class="headerlink" href="#werkzeug.contrib.sessions.Session.modified" title="Permalink to this definition">¶</a></dt>
<dd><p>Whenever an item on the cookie is set, this attribute is set to <cite>True</cite>.
However this does not track modifications inside mutable objects
in the session:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">Session</span><span class="p">({},</span> <span class="n">sid</span><span class="o">=</span><span class="s">'deadbeefbabe2c00ffee'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">c</span><span class="p">[</span><span class="s">"foo"</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">modified</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">modified</span> <span class="o">=</span> <span class="bp">False</span>
<span class="gp">>>> </span><span class="n">c</span><span class="p">[</span><span class="s">"foo"</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">modified</span>
<span class="go">False</span>
</pre></div>
</div>
<p>In that situation it has to be set to <cite>modified</cite> by hand so that
<a class="reference internal" href="#werkzeug.contrib.sessions.Session.should_save" title="werkzeug.contrib.sessions.Session.should_save"><tt class="xref py py-attr docutils literal"><span class="pre">should_save</span></tt></a> can pick it up.</p>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.contrib.sessions.Session.should_save">
<tt class="descname">should_save</tt><a class="headerlink" href="#werkzeug.contrib.sessions.Session.should_save" title="Permalink to this definition">¶</a></dt>
<dd><p>True if the session should be saved.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.6: </span>By default the session is now only saved if the session is
modified, not if it is new like it was before.</p>
</div>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="werkzeug.contrib.sessions.SessionStore">
<em class="property">class </em><tt class="descclassname">werkzeug.contrib.sessions.</tt><tt class="descname">SessionStore</tt><big>(</big><em>session_class=None</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore" title="Permalink to this definition">¶</a></dt>
<dd><p>Baseclass for all session stores. The Werkzeug contrib module does not
implement any useful stores besides the filesystem store, application
developers are encouraged to create their own stores.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>session_class</strong> – The session class to use. Defaults to
<a class="reference internal" href="#werkzeug.contrib.sessions.Session" title="werkzeug.contrib.sessions.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a>.</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.delete">
<tt class="descname">delete</tt><big>(</big><em>session</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete a session.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.generate_key">
<tt class="descname">generate_key</tt><big>(</big><em>salt=None</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.generate_key" title="Permalink to this definition">¶</a></dt>
<dd><p>Simple function that generates a new session key.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.get">
<tt class="descname">get</tt><big>(</big><em>sid</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a session for this sid or a new session object. This method
has to check if the session key is valid and create a new session if
that wasn’t the case.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.is_valid_key">
<tt class="descname">is_valid_key</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.is_valid_key" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if a key has the correct format.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.new">
<tt class="descname">new</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.new" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate a new session.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.save">
<tt class="descname">save</tt><big>(</big><em>session</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.save" title="Permalink to this definition">¶</a></dt>
<dd><p>Save a session.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.contrib.sessions.SessionStore.save_if_modified">
<tt class="descname">save_if_modified</tt><big>(</big><em>session</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionStore.save_if_modified" title="Permalink to this definition">¶</a></dt>
<dd><p>Save if a session class wants an update.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="werkzeug.contrib.sessions.FilesystemSessionStore">
<em class="property">class </em><tt class="descclassname">werkzeug.contrib.sessions.</tt><tt class="descname">FilesystemSessionStore</tt><big>(</big><em>path=None</em>, <em>filename_template='werkzeug_%s.sess'</em>, <em>session_class=None</em>, <em>renew_missing=False</em>, <em>mode=420</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.FilesystemSessionStore" title="Permalink to this definition">¶</a></dt>
<dd><p>Simple example session store that saves sessions on the filesystem.
This store works best on POSIX systems and Windows Vista / Windows
Server 2008 and newer.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.6: </span><cite>renew_missing</cite> was added. Previously this was considered <cite>True</cite>,
now the default changed to <cite>False</cite> and it can be explicitly
deactivated.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>path</strong> – the path to the folder used for storing the sessions.
If not provided the default temporary directory is used.</li>
<li><strong>filename_template</strong> – a string template used to give the session
a filename. <tt class="docutils literal"><span class="pre">%s</span></tt> is replaced with the
session id.</li>
<li><strong>session_class</strong> – The session class to use. Defaults to
<a class="reference internal" href="#werkzeug.contrib.sessions.Session" title="werkzeug.contrib.sessions.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a>.</li>
<li><strong>renew_missing</strong> – set to <cite>True</cite> if you want the store to
give the user a new sid if the session was
not yet saved.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="werkzeug.contrib.sessions.FilesystemSessionStore.list">
<tt class="descname">list</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.FilesystemSessionStore.list" title="Permalink to this definition">¶</a></dt>
<dd><p>Lists all sessions in the store.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6.</span></p>
</div>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="werkzeug.contrib.sessions.SessionMiddleware">
<em class="property">class </em><tt class="descclassname">werkzeug.contrib.sessions.</tt><tt class="descname">SessionMiddleware</tt><big>(</big><em>app</em>, <em>store</em>, <em>cookie_name='session_id'</em>, <em>cookie_age=None</em>, <em>cookie_expires=None</em>, <em>cookie_path='/'</em>, <em>cookie_domain=None</em>, <em>cookie_secure=None</em>, <em>cookie_httponly=False</em>, <em>environ_key='werkzeug.session'</em><big>)</big><a class="headerlink" href="#werkzeug.contrib.sessions.SessionMiddleware" title="Permalink to this definition">¶</a></dt>
<dd><p>A simple middleware that puts the session object of a store provided
into the WSGI environ. It automatically sets cookies and restores
sessions.</p>
<p>However a middleware is not the preferred solution because it won’t be as
fast as sessions managed by the application itself and will put a key into
the WSGI environment only relevant for the application which is against
the concept of WSGI.</p>
<p>The cookie parameters are the same as for the <tt class="xref py py-func docutils literal"><span class="pre">dump_cookie()</span></tt>
function just prefixed with <tt class="docutils literal"><span class="pre">cookie_</span></tt>. Additionally <cite>max_age</cite> is
called <cite>cookie_age</cite> and not <cite>cookie_max_age</cite> because of backwards
compatibility.</p>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html">
<img class="logo" src="../_static/werkzeug.png" alt="Logo"/>
</a></p>
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Sessions</a><ul>
<li><a class="reference internal" href="#application-integration">Application Integration</a></li>
<li><a class="reference internal" href="#reference">Reference</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Contributed Modules</a><ul>
<li>Previous: <a href="atom.html" title="previous chapter">Atom Syndication</a></li>
<li>Next: <a href="securecookie.html" title="next chapter">Secure Cookie</a></li>
</ul></li>
</ul></li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/contrib/sessions.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2011, The Werkzeug Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|