~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/web/howto/web-in-60/session-basics.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 Basics</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Session Basics</h1>
 
9
    <div class="toc"><ol/></div>
 
10
    <div class="content">
 
11
<span/>
 
12
 
 
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>
 
18
 
 
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>
 
27
 
 
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>
 
32
 
 
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
 
34
subclasses of it:</p>
 
35
 
 
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>
 
38
</pre>
 
39
 
 
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>
 
44
 
 
45
<pre class="python"><p class="py-linenumber">1
 
46
2
 
47
3
 
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>
 
51
</pre>
 
52
 
 
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>
 
56
method:</p>
 
57
 
 
58
<pre class="python"><p class="py-linenumber">1
 
59
2
 
60
3
 
61
4
 
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>
 
66
</pre>
 
67
 
 
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>
 
71
 
 
72
<pre class="python"><p class="py-linenumber">1
 
73
2
 
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">&quot;expire&quot;</span>, <span class="py-src-variable">ExpireSession</span>())
 
76
</pre>
 
77
 
 
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
 
83
a new session.</p>
 
84
 
 
85
<p>Here's the complete source for the example:</p>
 
86
 
 
87
<pre class="python"><p class="py-linenumber"> 1
 
88
 2
 
89
 3
 
90
 4
 
91
 5
 
92
 6
 
93
 7
 
94
 8
 
95
 9
 
96
10
 
97
11
 
98
12
 
99
13
 
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>
 
101
 
 
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>
 
105
 
 
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>
 
110
 
 
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">&quot;expire&quot;</span>, <span class="py-src-variable">ExpireSession</span>())
 
113
</pre>
 
114
 
 
115
</div>
 
116
 
 
117
    <p><a href="../index.html">Index</a></p>
 
118
    <span class="version">Version: 10.0.0</span>
 
119
  </body>
 
120
</html>
 
 
b'\\ No newline at end of file'