我正在学习 Udacity 的使用 Google Appengine 和 Python 进行 Web 开发的类(class)。
我想将 cookie 设置为过期,例如,自设置之日起 29 天。我如何实现这一目标?
我假设它是这样的:
def set_cookie(expire):
self.response.headers.add_header(
'Set-Cookie',
'Expires=%s; Path=/' % (expire_date))
“过期”值的格式是什么?如何将其到期时间设置为 + 29 天(或分钟、小时、周、月等)?
最佳答案
您不必手动添加 Set-Cookie
header ,而是可以执行以下操作:
import datetime
def yourFunction(...):
expireTime = datetime.datetime.now() #Check the docs, about adding 29 days, etc.
self.response.set_cookie('name', 'value', expires=expireTime, path='/', domain='example.com')
关于python - 使用 Google Appengine 设置 'Expires' cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20019074/