Python- bottle - cookies keep changing
Question: 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?


Answer: 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)
    

