Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- bottle - cookies keep changing

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?

like image 778
Tampa Avatar asked Mar 05 '26 02:03

Tampa


1 Answers

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)
like image 64
Lujeni Avatar answered Mar 07 '26 16:03

Lujeni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!