Python-瓶 - 饼干不断变化 [英] Python- bottle - cookies keep changing
本文介绍了Python-瓶 - 饼干不断变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我在瓶子中设置和读取Cookie的代码。
if request.get_cookie('mycookiename'):
cookie_id = request.get_cookie('mycookiename')
else:
cookie_id = str(uuid4())
response.set_cookie('mycookiename',cookie_id,max_age = 31556952 * 2 ,domain ='%s'%(cookie_domain))
当我去firefox和firebug可以看到cookie已设置。但是,当我刷新页面,我得到一个新的cookie。
解决方案
此代码工作。
在你的代码中,我猜你的else条件是不好的。
# - * - coding:utf-8 - * -
#!/ usr / bin / env python
uuid import uuid4
import bottle
@ bottle.route('/ cookie')
def cookie():
cookie_id = bottle.request.get_cookie('mycookiename ',str(uuid4()))
bottle.response.set_cookie('mycookiename',cookie_id)
返回'hello cookie'
如果__name__ =='__main__'
bottle.run(host ='localhost',port = 8080)
Below is my code for setting and reading cookies in bottle.
if request.get_cookie('mycookiename'):
cookie_id = request.get_cookie('mycookiename')
else:
cookie_id=str(uuid4())
response.set_cookie('mycookiename', cookie_id , max_age=31556952*2, domain='%s' % (cookie_domain))
When I go to firefox and firebug, I can see that the cookie is set. But, when I refresh the page, I get a new cookie. Every request is a new cookie id.
So, how do I resolve?
解决方案
This code works. You set a new value cookie with uuid4 if you have not a cookie already define.
In your code, i guess your "else" condition is bad.
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from uuid import uuid4
import bottle
@bottle.route('/cookie')
def cookie():
cookie_id = bottle.request.get_cookie('mycookiename', str(uuid4()))
bottle.response.set_cookie('mycookiename', cookie_id)
return 'hello cookie'
if __name__ == '__main__':
bottle.run(host='localhost', port=8080)
这篇关于Python-瓶 - 饼干不断变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文