Skip to content

Instantly share code, notes, and snippets.

@greatghoul
Created September 6, 2012 12:44
Show Gist options
  • Save greatghoul/3655878 to your computer and use it in GitHub Desktop.
Save greatghoul/3655878 to your computer and use it in GitHub Desktop.
Flask Session Expire
#-*- coding: utf-8 -*-
from flask import Flask, session, render_template_string
app = Flask(__name__)
app.config.update(
DEBUG = True,
SECRET_KEY = 'suckmycock!',
PERMANENT_SESSION_LIFETIME = 10)
@app.route('/session')
def show_session():
return render_template_string(u'''
{% if 'name' in session %}
name: {{ session.name }}
<hr/>
<div id="hint">session 将在 <strong id="time">{{ config.PERMANENT_SESSION_LIFETIME }}</strong> 秒后过期
<script type="text/javascript">
var timer = window.setInterval(function() {
var time_node = document.getElementById('time');
var time = parseInt(time_node.innerHTML);
if (time > 1) {
time_node.innerHTML = time - 1;
} else {
window.clearInterval(timer);
document.getElementById('hint').innerHTML =
'session 已过期,<a href="javascript:history.go()">刷新</a>以查看。';
}
}, 1000);
</script>
{% else %}
session 已经过期, 无法读取 <code>name</code> 属性,<a href="/">重新生成</a>.
{% endif %}''')
@app.route('/')
def make_session():
session.permanent = True
session['name'] = 'greatgargoyle'
return u'''已在 session 中创建 <code>name</code> 属性,<a href="/session">查看</a>'''
if __name__ == '__main__':
app.run()
@mattConn
Copy link

Interesting secret key!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment