tornado设置cookie过期时间(expires time)

具体的tornado设置过期时间的东西, 我也是查资料才发现的, 现在就贴代码吧

用户登录之后, 设置cookie, 我使用set_secure_cookie的, 它默认是有个30天的过期时间, 导致你关闭浏览器, 下次打开网站, 你还是登录状态.

然后过期时间想修改为, 关闭就失效, 答案很简单, 设置 expires_days=None, 就行了, 代码如下:

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. def set_current_user(self, user):  
  2.     # http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie  
  3.     # Pass expires_days=None to make it a session cookie (which expires when the browser is closed).  
  4.     self.set_secure_cookie('user_id', '1', expires_days=None)  


这里要注意的是不要同时传递expires参数给set_secure_cookie函数:

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. self.set_secure_cookie('user_id', '1', expires_days=None, expires=某个时间)  


不要这么搞, 直接不传递任何时间给expires, 否则, 就不会实现浏览器关闭就失效了.

问题来了, 那么设置cookie15分钟之后过期怎么办?

最开始, 我的尝试是 设置expires=15*60, 即设置expires=900, 

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. def set_current_user(self, user):  
  2.     # http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie  
  3.     # Pass expires_days=None to make it a session cookie (which expires when the browser is closed).  
  4.     self.set_secure_cookie('user_id', '1', expires_days=None, expires=900)  


好了, 问题来了, 登录之后, 马上转到用户主页, 然后接着马上就处于注销状态, 又转到登录页面了. 查看了http请求头信息, 发现过期的时间是1970年1月1日, 看来这个expires要设置为当前时间+额外的900秒.

新的尝试:

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. def set_current_user(self, user):  
  2.     import time  
  3.     # http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie  
  4.     # Pass expires_days=None to make it a session cookie (which expires when the browser is closed).  
  5.     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 plaincopy在CODE上查看代码片派生到我的代码片
 
  1. def set_current_user(self, user):  
  2.     import time  
  3.     # http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie  
  4.     # Pass expires_days=None to make it a session cookie (which expires when the browser is closed).  
  5.     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 plaincopy在CODE上查看代码片派生到我的代码片
 
  1. def set_current_user(self, user):  
  2.     import time  
  3.     # http://stackoverflow.com/questions/12383697/tornado-secure-cookie-expiration-aka-secure-session-cookie  
  4.     # Pass expires_days=None to make it a session cookie (which expires when the browser is closed).  
  5.     self.set_secure_cookie('user_id', '1', expires=time.time()+900)  


到这里, tornado设置cookie的过期时间, 就只有这些内容了, 其他的请看tornado的官方文档和源码. 不足之处, 还请留言评论.

原文地址:https://www.cnblogs.com/apexchu/p/4363250.html