tornado设置cookie过期时间(expires time)
具体的tornado设置过期时间的东西, 我也是查资料才发现的, 现在就贴代码吧
用户登录之后, 设置cookie, 我使用set_secure_cookie的, 它默认是有个30天的过期时间, 导致你关闭浏览器, 下次打开网站, 你还是登录状态.
然后过期时间想修改为, 关闭就失效, 答案很简单, 设置 expires_days=None, 就行了, 代码如下:
- 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函数:
- self.set_secure_cookie('user_id', '1', expires_days=None, expires=某个时间)
不要这么搞, 直接不传递任何时间给expires, 否则, 就不会实现浏览器关闭就失效了.
问题来了, 那么设置cookie15分钟之后过期怎么办?
最开始, 我的尝试是 设置expires=15*60, 即设置expires=900,
- 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秒.
新的尝试:
- 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(), 貌似有效了
- 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分钟可以简化为:
- 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的官方文档和源码. 不足之处, 还请留言评论.
tornado设置cookie过期时间(expires time)的更多相关文章
- 五十九:Flask.Cookie之flask设置cookie过期时间
设置cookie有效期1.max_age:距离现在多少秒后过期,在IE8以下不支持2.expires:datatime类型,使用此参数,需参照格林尼治时间,即北京时间-8个小时3.如果max_age和 ...
- 设置COOKIE过期时间的方法
第一,日期运算法 1)将期限设置为当前日期后的第N天的0时0分0秒 Response.Cookies(LastView).Expires=dateadd(d,N,date) 2)将期限设置为当前日期后 ...
- JS设置Cookie过期时间
//JS操作cookies方法! //写cookies function setCookie(name,value) { var Days = 30; var exp = new Date(); ex ...
- 设置 cookie过期时间
cookie.setMaxAge(0);//不记录cookie cookie.setMaxAge(-1);//会话级cookie,关闭浏览器失效 cookie.setMaxAge(60*60);//过 ...
- 如何设置session过期时间为30分钟
今天在我的微博(Laruence)上发出一个问题: 我在面试的时候, 经常会问一个问题: “如何设置一个30分钟过期的Session?”, 大家不要觉得看似简单, 这里面包含的知识挺多, 特别适合考察 ...
- Tornado中Cookie过期问题
首先,web应用程序是使用HTTP协议进行数据传输,因为HTTP协议是无状态的,所以一旦提交数据完成后,客户端和服务器端的连接就会被关闭,再次进行数据的交换就得重新建立新的连接,那么,有个问题就是服务 ...
- Tornado的cookie过期问题
首先,web应用程序是使用HTTP协议进行数据传输,因为HTTP协议是无状态的,所以一旦提交数据完成后,客户端和服务器端的连接就会被关闭,再次进行数据的交换就得重新建立新的连接,那么,有个问题就是服务 ...
- js设置cookie过期及清除浏览器对应名称的cookie
js设置cookie过期也就相当于清除浏览器对应名称的cookie的例子. 代码: function ClearCookie() { var expires = new Date(); expir ...
- php中实现精确设置session过期时间的方法
http://www.jb51.net/article/52309.htm 大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设置一下sessio ...
随机推荐
- Java-Runoob:Java 基本数据类型
ylbtech-Java-Runoob:Java 基本数据类型 1.返回顶部 1. Java 基本数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根 ...
- jgrid相关功能用法记录
1.获取行号var ids = $gridList.jqGrid('getGridParam', 'selarrrow'); //多选,返回选中行号组字符 var ids2 = $gridList.j ...
- http协议Keep-Alive
Keep-Alive 是什么? 概观 默认情况下,HTTP链接通常在请求完成之后关闭.这意味着服务端在完成响应的交付之后便关闭了TCP链接.为了让链接保持打开,来满足多请求,可以使用keep-aliv ...
- Django界面不能添加中文解决办法
Django项目部署好后,界面添加中文会报错,解决办法: 创建数据库时要指定编码格式: CREATE DATABASE blog CHARACTER SET utf8; 如果已经创建完毕则修改: al ...
- keil的使用:新建Project
新建项目--->新建文件夹----->把新建的项目放在自己的文件夹中------>选择开发板------>添加开发板的驱动文件---->main函数 项目分组基本如图,S ...
- ubuntu 12.04的源更新
apt-get install vim 安装vim vim /etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ precise main ...
- C++深度解析教程学习笔记(3)函数的扩展
1.内联函数 1.1.常量与宏的回顾 (1)C++中的 const 常量可以替代宏常数定义,如: ; //等价于 #define A 3 (2)C++中是否有解决方案,可以用来替代宏代码片段呢? 1. ...
- linux tcpdump补充
If they are going across the loopback interface, you may have to tell tcpdump to read that interface ...
- angular.js简单入门。
小弟刚接触angular js 就写了一个简单的入门.后续慢慢补... 首先看 html 页面. <html> <meta charset="UTF-8"> ...
- AntD01 Angular2整合AntD、Angular2整合Material、利用Angular2,AntD,Material联合打造后台管理系统 ???
待更新... 2018-5-21 13:53:52 1 环境说明 2 搭建Angular项目 详情参见 -> 点击前往 辅助技能 -> 点击前往 3 创建共享模块 ng g m share ...