1
<?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">
3
<title>Twisted Documentation: Session Basics</title>
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Session Basics</h1>
9
<div class="toc"><ol/></div>
13
<p>Sessions are the most complicated topic covered in this series of examples,
14
and because of that it is going to take a few examples to cover all of the
15
different aspects. This first example demonstrates the very basics of the
16
Twisted Web session API: how to get the session object for the current request
17
and how to prematurely expire a session.</p>
19
<p>Before diving into the APIs, let's look at the big picture of sessions in
20
Twisted Web. Sessions are represented by instances of <code class="API"><a href="http://twistedmatrix.com/documents/10.0.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/10.0.0/api/twisted.web.server.Site.html" title="twisted.web.server.Site">Site</a></code> creates a new instance of
21
<code>Session</code> the first time an application asks for it for a particular
22
session. <code>Session</code> instances are kept on the <code>Site</code>
23
instance until they expire (due to inactivity or because they are explicitly
24
expired). Each time after the first that a particular session's
25
<code>Session</code> object is requested, it is retrieved from
26
the <code>Site</code>.</p>
28
<p>With the conceptual underpinnings of the upcoming API in place, here comes
29
the example. This will be a very simple <a href="rpy-scripts.html" shape="rect">rpy
30
script</a> which tells a user what its unique session identifier is and lets it
31
prematurely expire the session.</p>
33
<p>First, we'll import <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">Resource</a></code> so we can define a couple of
36
<pre class="python"><p class="py-linenumber">1
37
</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>
40
<p>Next we'll define the resource which tells the client what its session
41
identifier is. This is done easily by first getting the session object
42
using <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Request.getSession.html" title="twisted.web.server.Request.getSession">Request.getSession</a></code> and
43
then getting the session object's uid attribute:</p>
45
<pre class="python"><p class="py-linenumber">1
48
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">ShowSession</span>(<span class="py-src-parameter">Resource</span>):
49
<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>):
50
<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>
53
<p>To let the client expire its own session before it times out, we'll define
54
another resource which expires whatever session it is requested with. This is
55
done using the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Session.expire.html" title="twisted.web.server.Session.expire">Session.expire</a></code>
58
<pre class="python"><p class="py-linenumber">1
62
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">ExpireSession</span>(<span class="py-src-parameter">Resource</span>):
63
<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>):
64
<span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>().<span class="py-src-variable">expire</span>()
65
<span class="py-src-keyword">return</span> <span class="py-src-string">'Your session has been expired.'</span>
68
<p>Finally, to make the example an rpy script, we'll make an instance of
69
<code>ShowSession</code> and give it an instance of <code>ExpireSession</code>
70
as a child using <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.Resource.putChild.html" title="twisted.web.resource.Resource.putChild">Resource.putChild</a></code>:</p>
72
<pre class="python"><p class="py-linenumber">1
74
</p><span class="py-src-variable">resource</span> = <span class="py-src-variable">ShowSession</span>()
75
<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>())
78
<p>And that is the complete example. You can fire this up and load the top
79
page. You'll see a (rather opaque) session identifier that remains the same
80
across reloads (at least until you flush the <code>TWISTED_SESSION</code> cookie
81
from your browser or enough time passes). You can then visit
82
the <code>expire</code> child and go back to the top page and see that you have
85
<p>Here's the complete source for the example:</p>
87
<pre class="python"><p class="py-linenumber"> 1
100
</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>
102
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ShowSession</span>(<span class="py-src-parameter">Resource</span>):
103
<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>):
104
<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>
106
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ExpireSession</span>(<span class="py-src-parameter">Resource</span>):
107
<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>):
108
<span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>().<span class="py-src-variable">expire</span>()
109
<span class="py-src-keyword">return</span> <span class="py-src-string">'Your session has been expired.'</span>
111
<span class="py-src-variable">resource</span> = <span class="py-src-variable">ShowSession</span>()
112
<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>())
117
<p><a href="../index.html">Index</a></p>
118
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'