Tornaodo 设置cookie超时(Expires)
2016-02-24 13:59
211 查看
具体的tornado设置过期时间的东西, 我也是查资料才发现的, 现在就贴代码吧
用户登录之后, 设置cookie, 我使用set_secure_cookie的, 它默认是有个30天的过期时间, 导致你关闭浏览器, 下次打开网站, 你还是登录状态.
然后过期时间想修改为, 关闭就失效, 答案很简单, 设置 expires_days=None, 就行了, 代码如下:
[python] view
plain copy
def set_current_user(self, user):
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None)
这里要注意的是不要同时传递expires参数给set_secure_cookie函数:
[python] view
plain copy
self.set_secure_cookie('user_id', '1', expires_days=None, expires=某个时间)
不要这么搞, 直接不传递任何时间给expires, 否则, 就不会实现浏览器关闭就失效了.
问题来了, 那么设置cookie15分钟之后过期怎么办?
最开始, 我的尝试是 设置expires=15*60, 即设置expires=900,
[python] view
plain copy
def set_current_user(self, user):
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None, expires=900)
好了, 问题来了, 登录之后, 马上转到用户主页, 然后接着马上就处于注销状态, 又转到登录页面了. 查看了http请求头信息, 发现过期的时间是1970年1月1日, 看来这个expires要设置为当前时间+额外的900秒.
新的尝试:
[python] view
plain copy
def set_current_user(self, user):
import time
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None, expires=time.mktime(time.gmtime())+900)
使用 time.mktime(time.gmtime()) 是想获取gmt(国际标准时间), 因为我发现过期时间, 在浏览器中查看是gmt格式的. 结果这种设置, 还是不行, 我这里是有8个小时的差别, 应该是和时区有关系.
后来我改为下面这种直接使用time.time(), 貌似有效了
[python] view
plain copy
def set_current_user(self, user):
import time
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None, expires=time.time()+900)
这里要说明的是, expires_day=None, 或者expires_day=3, 即3天, 都不会影响expires的, 因为expires比expires_days 的优先级高些. 所以这里设置为15分钟可以简化为:
[python] view
plain copy
def set_current_user(self, user):
import time
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires=time.time()+900)
到这里, tornado设置cookie的过期时间, 就只有这些内容了, 其他的请看tornado的官方文档和源码. 不足之处, 还请留言评论.
用户登录之后, 设置cookie, 我使用set_secure_cookie的, 它默认是有个30天的过期时间, 导致你关闭浏览器, 下次打开网站, 你还是登录状态.
然后过期时间想修改为, 关闭就失效, 答案很简单, 设置 expires_days=None, 就行了, 代码如下:
[python] view
plain copy
def set_current_user(self, user):
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None)
这里要注意的是不要同时传递expires参数给set_secure_cookie函数:
[python] view
plain copy
self.set_secure_cookie('user_id', '1', expires_days=None, expires=某个时间)
不要这么搞, 直接不传递任何时间给expires, 否则, 就不会实现浏览器关闭就失效了.
问题来了, 那么设置cookie15分钟之后过期怎么办?
最开始, 我的尝试是 设置expires=15*60, 即设置expires=900,
[python] view
plain copy
def set_current_user(self, user):
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None, expires=900)
好了, 问题来了, 登录之后, 马上转到用户主页, 然后接着马上就处于注销状态, 又转到登录页面了. 查看了http请求头信息, 发现过期的时间是1970年1月1日, 看来这个expires要设置为当前时间+额外的900秒.
新的尝试:
[python] view
plain copy
def set_current_user(self, user):
import time
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None, expires=time.mktime(time.gmtime())+900)
使用 time.mktime(time.gmtime()) 是想获取gmt(国际标准时间), 因为我发现过期时间, 在浏览器中查看是gmt格式的. 结果这种设置, 还是不行, 我这里是有8个小时的差别, 应该是和时区有关系.
后来我改为下面这种直接使用time.time(), 貌似有效了
[python] view
plain copy
def set_current_user(self, user):
import time
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires_days=None, expires=time.time()+900)
这里要说明的是, expires_day=None, 或者expires_day=3, 即3天, 都不会影响expires的, 因为expires比expires_days 的优先级高些. 所以这里设置为15分钟可以简化为:
[python] view
plain copy
def set_current_user(self, user):
import time
# http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie
# Pass expires_days=None to make it a session cookie (which expires when the browser is closed).
self.set_secure_cookie('user_id', '1', expires=time.time()+900)
到这里, tornado设置cookie的过期时间, 就只有这些内容了, 其他的请看tornado的官方文档和源码. 不足之处, 还请留言评论.
相关文章推荐
- Can’t connect to local MySQL server through socket
- 知道怎样读书和选书?
- HR专业资料—免费下载
- 关于空值null的排序问题
- gin中的断言写法
- lintcode-easy-Count 1 in Binary
- jaxb工具类
- JavaScript的设计模式经典之代理模式
- Jps介绍以及解决jps无法查看某个已经启动的java进程问题
- CentOS RPM安装MySQL 5.6修改默认密码
- C/C++——求下面数据类型的最大值和最小值: char, short, int, long, float, double, long double和numeric_limits使用
- 探索PinYin4j.jar将汉字转换为拼音的基本用法
- kidd风的IOS日志之多线程(NSThread)的使用
- 微服务实战之微服务介绍
- 算法导论------------桶排序算法之研究
- 在win7/8/10鼠标右键添加“管理员取得所有权”
- QuickSort 递归 分治
- Git用法之分支管理和分支开发工作流
- new (std::nothrow) 与 new
- js刷新页面的几种方法