-
Notifications
You must be signed in to change notification settings - Fork 0
/
expire-a-flask-session.html
139 lines (123 loc) · 7.89 KB
/
expire-a-flask-session.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta content='這世界上沒有不寂寞的大人' property='og:title' />
<meta content='width=device-width, initial-scale=1.0, user-scalable=no' name='viewport'>
<meta content='text/html; charset=utf-8' http-equiv='content-type' />
<meta content="http://muan.co/images/og.png" property="og:image" />
<meta content='http://hondajojo.github.io/expire-a-flask-session.html' property='og:url' />
<meta content="import os from datetime import timedelta from flask import Flask, session, render_template app = Flask(__name__) ..." property='og:description' />
<meta content="article" property="og:type" />
<title>這世界上沒有不寂寞的大人</title>
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700' rel='stylesheet' type='text/css'>
<link href='http://hondajojo.github.io/theme/images/fav.png' rel='shortcut icon'>
<link rel="stylesheet" href="http://hondajojo.github.io/theme/syntax.css" type="text/css" />
<link rel="stylesheet" href="http://hondajojo.github.io/theme/style.css" type="text/css" />
<link rel="stylesheet" href="http://hondajojo.github.io/theme/responsive.css" type="text/css" />
<link rel="stylesheet" href="http://hondajojo.github.io/theme/stuff/style.css" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="content-wrapper">
<section class="content">
<p><a href="http://hondajojo.github.io/" class="hex-shape big"></a></p>
<hr />
<article>
<h3 class="reset bold a">flask 设置session过期时间</h3>
<small class="datetime muted">2015-11-06</small>
<hr>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span>
<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">render_template</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">secret_key</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">urandom</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">permanent_session_lifetime</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
<span class="n">session</span><span class="o">.</span><span class="n">permanent</span> <span class="o">=</span> <span class="bp">True</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">session</span><span class="p">[</span><span class="s">'counter'</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
<span class="n">session</span><span class="p">[</span><span class="s">'counter'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="s">"Number of reloads on the current session: </span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="n">session</span><span class="p">[</span><span class="s">'counter'</span><span class="p">]</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">debug</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
<h3>参数详解</h3>
<p><code>permanent_session_lifetime</code> - session过期时间</p>
<blockquote>
<ul>
<li><a href="http://flask.pocoo.org/docs/0.10/api/#flask.Flask.permanent_session_lifetime">文档</a></li>
<li>默认31天</li>
<li>flask.Flask的属性,所以<code>flask.Flask.permanent_session_lifetime = timedelta(seconds=15)</code>来赋值</li>
<li>也可在config.py里赋值<code>PERMANENT_SESSION_LIFETIME = 15</code></li>
</ul>
</blockquote>
<p><code>permanent</code></p>
<blockquote>
<ul>
<li><a href="http://flask.pocoo.org/docs/0.10/api/#flask.session.permanent">文档</a></li>
<li>默认为<code>False</code>,意思是关掉浏览器就删除session,不管<code>permanent_session_lifetime</code>是否到达</li>
<li>若设置为<code>True</code>,关掉浏览器,如果<code>permanent_session_lifetime</code>未到,则再次打开浏览器,session也会存在,如登陆状态也在</li>
<li>flask.session的属性,<code>session.permanent = True</code>来定义</li>
</ul>
</blockquote>
<h3>例子解析</h3>
<blockquote>
<ul>
<li>因为<code>session.permanent = True</code>,不管是否关闭浏览器,在浏览器没有活动超过10s,session就被删除</li>
<li>如果<code>session.permanent = Flase</code>即默认,有两种情况session会被删除,一种是关闭了浏览器,第二种是未关闭浏览器,但是未活动时间超过10s</li>
</ul>
</blockquote>
</article>
<div class="signoff">
<p>
Til next time,<br/>
<div class="avatar-wrapper"><img src="http://hondajojo.github.io/theme/images/avatar.png" class="avatar" /></div>
maijver
<span class="muted">at 09:11</span>
</p>
</div>
<hr />
<div class="block">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = "hondajojo";
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>
</div>
<footer>
<hr />
<p class="small bold">
© maijver. All Rights Reserved.<br />
Powered by <a href="https://github.com/getpelican/pelican" target="_blank">Pelican</a>. Theme by <a href="https://github.com/lord63/pelican-scribble-hex" target="_blank">Pelican-scribble-hex</a>.
</p>
<div class="hex-shape small muted"></div>
</footer>
</div>
<script src='http://hondajojo.github.io/theme/jquery.js' type='text/javascript'></script>
<script src='http://hondajojo.github.io/theme/pd.js' type='text/javascript'></script>
<script src='http://hondajojo.github.io/theme/stuff.js' type='text/javascript'></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<!-- google analytics -->
<script type='text/javascript'>
//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
//]]>
</script>
</body>
</html>