How to set multiple cookies using CherryPy

From CherryPy's documentation , there seems to be only one cookie slot. Here is my sample code

def sendCookie(self):
    cookie = cherrypy.response.cookie
    cookie['name'] = 'Chips Ahoy!'
    return 'Cookie is now in your hands.'
sendCookie.exposed = True

I want to set some cookies. I think about it, but of course it will just overwrite the first setup.

def sendCookie(self):
    cookie = cherrypy.response.cookie
    cookie2 = cherrypy.response.cookie
    cookie['name'] = 'Chips Ahoy!'
    cookie2['name'] = 'Chocolate Chips'
    return 'Cookie is now in your hands.'
sendCookie.exposed = True

How to set multiple cookies using CherryPy?

+5
source share
1 answer

I think the first key in cookieshould match the cookie name, where the extra keys will match the attributes of this cookie. Thus, instead of using 'name'as a key for your cookies, you should use some unique name.

def sendCookie(self):
    cookies = cherrypy.response.cookie

    cookies['cookie1'] = 'Chips Ahoy!'
    cookies['cookie1']['path'] = '/the/red/bag/'
    cookies['cookie1']['comment'] = 'round'

    cookies['cookie2'] = 'Chocolate Chips'
    cookies['cookie2']['path'] = '/the/yellow/bag/'
    cookies['cookie2']['comment'] = 'thousands'

    return 'Cookies are now in your hands.'
setCookie.exposed = True

It works?

: , morsel , ('shape' 'count'). .

+5

All Articles