如何使用GAE / Python设置Cookie一个月? [英] How to set cookies with GAE/Python for 1 month?

查看:262
本文介绍了如何使用GAE / Python设置Cookie一个月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现以下内容:


  1. 用户输入用户ID和密码


  2. 如果正确,则应将这些详细信息的Cookie保存一个月

  3. 每次用户使用我的网站时,应查找Cookie

  4. 如果未找到 - 请转到步骤1

  1. User input user id and pass
  2. We validate that on another server
  3. If they are correct, cookies with these details should be saved for one month
  4. Each time user uses my site, we should look for cookies
  5. If they are not found - go to step 1

我将Cookie设置为1个月?

How can I set cookies for 1 month?

以下是否会工作?

self.response.headers.add_header(
        'Set-Cookie', 
        'credentials=%s; expires=Fri, 31-Dec-2020 23:59:59 GMT' \
          % credentials.encode())

如何计算一个月后所需的格式? / p>

How to calculate one month from now in the required format?

推荐答案

您可以使用webapp.Response.set_cookie()方法:

You can use webapp.Response.set_cookie() method:

import datetime

self.response.set_cookie('name', 'value', expires=datetime.datetime.now(), path='/', domain='example.com')

Cookie的格式设置日期如下:

Formatting dates for cookies is something like this:

print (datetime.datetime.now() + datetime.timedelta(weeks=4)).strftime('%a, %d %b %Y %H:%M:%S GMT') 

这篇关于如何使用GAE / Python设置Cookie一个月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆