~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/web/howto/web-in-60/session-endings.html

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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">
 
2
  <head>
 
3
<title>Twisted Documentation: Session Endings</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Session Endings</h1>
 
9
    <div class="toc"><ol/></div>
 
10
    <div class="content">
 
11
<span/>
 
12
 
 
13
<p>The previous two examples introduced Twisted Web's session APIs. This
 
14
included accessing the session object, storing state on it, and retrieving it
 
15
later, as well as the idea that the <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> object has a lifetime which is tied to
 
16
the notional session it represents. This example demonstrates how to exert some
 
17
control over that lifetime and react when it expires.</p>
 
18
 
 
19
<p>The lifetime of a session is controlled by the <code>sessionTimeout</code>
 
20
attribute of the <code>Session</code> class. This attribute gives the number of
 
21
seconds a session may go without being accessed before it expires. The default
 
22
is 15 minutes. In this example we'll change that to a different value.</p>
 
23
 
 
24
<p>One way to override the value is with a subclass:</p>
 
25
 
 
26
<pre class="python"><p class="py-linenumber">1
 
27
2
 
28
3
 
29
4
 
30
</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">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Session</span>
 
31
 
 
32
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ShortSession</span>(<span class="py-src-parameter">Session</span>):
 
33
    <span class="py-src-variable">sessionTimeout</span> = <span class="py-src-number">60</span>
 
34
</pre>
 
35
 
 
36
<p>To have Twisted Web actually make use of this session class, rather than the
 
37
default, it is also necessary to override the <code>sessionFactory</code> attribute of
 
38
<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>. We could do this with
 
39
another subclass, but we could also do it to just one instance
 
40
of <code>Site</code>:</p>
 
41
 
 
42
<pre class="python"><p class="py-linenumber">1
 
43
2
 
44
3
 
45
4
 
46
</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">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Site</span>
 
47
 
 
48
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">rootResource</span>)
 
49
<span class="py-src-variable">factory</span>.<span class="py-src-variable">sessionFactory</span> = <span class="py-src-variable">ShortSession</span>
 
50
</pre>
 
51
 
 
52
<p>Sessions given out for requests served by this <code>Site</code> will
 
53
use <code>ShortSession</code> and only last one minute without activity.</p>
 
54
 
 
55
<p>You can have arbitrary functions run when sessions expire, too. This can be
 
56
useful for cleaning up external resources associated with the session, tracking
 
57
usage statistics, and more. This functionality is provided via
 
58
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Session.notifyOnExpire.html" title="twisted.web.server.Session.notifyOnExpire">Session.notifyOnExpire</a></code>. It
 
59
accepts a single argument: a function to call when the session expires. Here's a
 
60
trivial example which prints a message whenever a session expires:</p>
 
61
 
 
62
<pre class="python"><p class="py-linenumber"> 1
 
63
 2
 
64
 3
 
65
 4
 
66
 5
 
67
 6
 
68
 7
 
69
 8
 
70
 9
 
71
10
 
72
11
 
73
12
 
74
13
 
75
14
 
76
15
 
77
</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>
 
78
 
 
79
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ExpirationLogger</span>(<span class="py-src-parameter">Resource</span>):
 
80
    <span class="py-src-variable">sessions</span> = <span class="py-src-variable">set</span>()
 
81
 
 
82
    <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>):
 
83
        <span class="py-src-variable">session</span> = <span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>()
 
84
        <span class="py-src-keyword">if</span> <span class="py-src-variable">session</span>.<span class="py-src-variable">uid</span> <span class="py-src-keyword">not</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">sessions</span>:
 
85
            <span class="py-src-variable">self</span>.<span class="py-src-variable">sessions</span>.<span class="py-src-variable">add</span>(<span class="py-src-variable">session</span>.<span class="py-src-variable">uid</span>)
 
86
            <span class="py-src-variable">session</span>.<span class="py-src-variable">notifyOnExpire</span>(<span class="py-src-keyword">lambda</span>: <span class="py-src-variable">self</span>.<span class="py-src-variable">_expired</span>(<span class="py-src-variable">session</span>.<span class="py-src-variable">uid</span>))
 
87
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;&quot;</span>
 
88
 
 
89
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">_expired</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">uid</span>):
 
90
        <span class="py-src-keyword">print</span> <span class="py-src-string">&quot;Session&quot;</span>, <span class="py-src-variable">uid</span>, <span class="py-src-string">&quot;has expired.&quot;</span>
 
91
        <span class="py-src-variable">self</span>.<span class="py-src-variable">sessions</span>.<span class="py-src-variable">remove</span>(<span class="py-src-variable">uid</span>)
 
92
</pre>
 
93
 
 
94
<p>Keep in mind that using a method as the callback will keep the instance (in
 
95
this case, the <code>ExpirationLogger</code> resource) in memory until the
 
96
session expires.</p>
 
97
 
 
98
<p>With those pieces in hand, here's an example that prints a message whenever a
 
99
session expires, and uses sessions which last for 5 seconds:</p>
 
100
 
 
101
<pre class="python"><p class="py-linenumber"> 1
 
102
 2
 
103
 3
 
104
 4
 
105
 5
 
106
 6
 
107
 7
 
108
 8
 
109
 9
 
110
10
 
111
11
 
112
12
 
113
13
 
114
14
 
115
15
 
116
16
 
117
17
 
118
18
 
119
19
 
120
20
 
121
21
 
122
22
 
123
23
 
124
24
 
125
25
 
126
26
 
127
27
 
128
28
 
129
</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">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Site</span>, <span class="py-src-variable">Session</span>
 
130
<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>
 
131
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
 
132
 
 
133
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ShortSession</span>(<span class="py-src-parameter">Session</span>):
 
134
    <span class="py-src-variable">sessionTimeout</span> = <span class="py-src-number">5</span>
 
135
 
 
136
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ExpirationLogger</span>(<span class="py-src-parameter">Resource</span>):
 
137
    <span class="py-src-variable">sessions</span> = <span class="py-src-variable">set</span>()
 
138
 
 
139
    <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>):
 
140
        <span class="py-src-variable">session</span> = <span class="py-src-variable">request</span>.<span class="py-src-variable">getSession</span>()
 
141
        <span class="py-src-keyword">if</span> <span class="py-src-variable">session</span>.<span class="py-src-variable">uid</span> <span class="py-src-keyword">not</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">sessions</span>:
 
142
            <span class="py-src-variable">self</span>.<span class="py-src-variable">sessions</span>.<span class="py-src-variable">add</span>(<span class="py-src-variable">session</span>.<span class="py-src-variable">uid</span>)
 
143
            <span class="py-src-variable">session</span>.<span class="py-src-variable">notifyOnExpire</span>(<span class="py-src-keyword">lambda</span>: <span class="py-src-variable">self</span>.<span class="py-src-variable">_expired</span>(<span class="py-src-variable">session</span>.<span class="py-src-variable">uid</span>))
 
144
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;&quot;</span>
 
145
 
 
146
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">_expired</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">uid</span>):
 
147
        <span class="py-src-keyword">print</span> <span class="py-src-string">&quot;Session&quot;</span>, <span class="py-src-variable">uid</span>, <span class="py-src-string">&quot;has expired.&quot;</span>
 
148
        <span class="py-src-variable">self</span>.<span class="py-src-variable">sessions</span>.<span class="py-src-variable">remove</span>(<span class="py-src-variable">uid</span>)
 
149
 
 
150
<span class="py-src-variable">rootResource</span> = <span class="py-src-variable">Resource</span>()
 
151
<span class="py-src-variable">rootResource</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;logme&quot;</span>, <span class="py-src-variable">ExpirationLogger</span>())
 
152
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">rootResource</span>)
 
153
<span class="py-src-variable">factory</span>.<span class="py-src-variable">sessionFactory</span> = <span class="py-src-variable">ShortSession</span>
 
154
 
 
155
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8080</span>, <span class="py-src-variable">factory</span>)
 
156
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
157
</pre>
 
158
 
 
159
<p>Since <code>Site</code> customization is required, this example can't be
 
160
rpy-based, so it brings back the manual <code>reactor.listenTCP</code>
 
161
and <code>reactor.run</code> calls. Run it and visit <code>/logme</code> to see
 
162
it in action. Keep visiting it to keep your session active. Stop visiting it for
 
163
five seconds to see your session expiration message.</p>
 
164
 
 
165
</div>
 
166
 
 
167
    <p><a href="../index.html">Index</a></p>
 
168
    <span class="version">Version: 10.0.0</span>
 
169
  </body>
 
170
</html>
 
 
b'\\ No newline at end of file'