Python- bottle - cookies keep changing

牧云@^-^@ 提交于 2019-12-14 04:18:25

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/11730580/python-bottle-cookies-keep-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!