/usr/share/doc/python-twisted-web/howto/web-in-60/session-basics.html is in python-twisted-web 13.2.0-1ubuntu1.
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 | <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Twisted Documentation: Session Basics</title>
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body bgcolor="white">
<h1 class="title">Session Basics</h1>
<div class="toc"><ol/></div>
<div class="content">
<span/>
<p>Sessions are the most complicated topic covered in this series of examples,
and because of that it is going to take a few examples to cover all of the
different aspects. This first example demonstrates the very basics of the
Twisted Web session API: how to get the session object for the current request
and how to prematurely expire a session.</p>
<p>Before diving into the APIs, let's look at the big picture of
sessions in Twisted Web. Sessions are represented by instances
of <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.server.Session.html" title="twisted.web.server.Session">Session</a></code>. The <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.server.Site.html" title="twisted.web.server.Site">Site</a></code> creates a new instance
of <code>Session</code> the first time an application asks for it for
a particular session. <code>Session</code> instances are kept on
the <code>Site</code> instance until they expire (due to inactivity or
because they are explicitly expired). Each time after the first that a
particular session's <code>Session</code> object is requested, it is
retrieved from the <code>Site</code>.</p>
<p>With the conceptual underpinnings of the upcoming API in place, here comes
the example. This will be a very simple <a href="rpy-scripts.html" shape="rect">rpy
script</a> which tells a user what its unique session identifier is and lets it
prematurely expire the session.</p>
<p>First, we'll import <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">Resource</a></code> so we can define a couple of
subclasses of it:</p>
<pre class="python"><p class="py-linenumber">1
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
</pre>
<p>Next we'll define the resource which tells the client what its session
identifier is. This is done easily by first getting the session object
using <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.server.Request.getSession.html" title="twisted.web.server.Request.getSession">Request.getSession</a></code> and
then getting the session object's uid attribute:</p>
<pre class="python"><p class="py-linenumber">1
2
3
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">ShowSession</span>(<span class="py-src-parameter">Resource</span>):
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-keyword">return</span> <span class="py-src-string">'Your session id is: '</span> + <span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>().<span class="py-src-variable">uid</span>
</pre>
<p>To let the client expire its own session before it times out, we'll define
another resource which expires whatever session it is requested with. This is
done using the <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.server.Session.expire.html" title="twisted.web.server.Session.expire">Session.expire</a></code>
method:</p>
<pre class="python"><p class="py-linenumber">1
2
3
4
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">ExpireSession</span>(<span class="py-src-parameter">Resource</span>):
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>().<span class="py-src-variable">expire</span>()
<span class="py-src-keyword">return</span> <span class="py-src-string">'Your session has been expired.'</span>
</pre>
<p>Finally, to make the example an rpy script, we'll make an instance
of <code>ShowSession</code> and give it an instance
of <code>ExpireSession</code> as a child using <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.resource.Resource.putChild.html" title="twisted.web.resource.Resource.putChild">Resource.putChild</a></code>:</p>
<pre class="python"><p class="py-linenumber">1
2
</p><span class="py-src-variable">resource</span> = <span class="py-src-variable">ShowSession</span>()
<span class="py-src-variable">resource</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"expire"</span>, <span class="py-src-variable">ExpireSession</span>())
</pre>
<p>And that is the complete example. You can fire this up and load the top
page. You'll see a (rather opaque) session identifier that remains the same
across reloads (at least until you flush the <code>TWISTED_SESSION</code> cookie
from your browser or enough time passes). You can then visit
the <code>expire</code> child and go back to the top page and see that you have
a new session.</p>
<p>Here's the complete source for the example:</p>
<pre class="python"><p class="py-linenumber"> 1
2
3
4
5
6
7
8
9
10
11
12
13
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ShowSession</span>(<span class="py-src-parameter">Resource</span>):
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-keyword">return</span> <span class="py-src-string">'Your session id is: '</span> + <span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>().<span class="py-src-variable">uid</span>
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ExpireSession</span>(<span class="py-src-parameter">Resource</span>):
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>().<span class="py-src-variable">expire</span>()
<span class="py-src-keyword">return</span> <span class="py-src-string">'Your session has been expired.'</span>
<span class="py-src-variable">resource</span> = <span class="py-src-variable">ShowSession</span>()
<span class="py-src-variable">resource</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"expire"</span>, <span class="py-src-variable">ExpireSession</span>())
</pre>
</div>
<p><a href="../index.html">Index</a></p>
<span class="version">Version: 13.2.0</span>
</body>
</html>
|