如何使用CherryPy设置多个cookie

6

根据CherryPy 文档,似乎只有一个cookie插槽。这是我的示例代码:

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

我想设置多个cookie。我考虑采用以下方式,但这只会覆盖第一个设置。
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

如何使用CherryPy设置多个cookie?
1个回答

6

我认为cookie中的第一个关键字应该对应于cookie的名称,而其他关键字则应对应于该cookie的属性。因此,您不应该使用'name'作为cookie的关键字,而应使用一些唯一的名称。

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

这能行吗?

编辑:糟糕,每个morsel都有一组预定义的属性,而我正在定义自己的('shape''count')。现在应该已经修复了。


不,只有一个 cookie 被设置了。在这种情况下,只有 cookie2cookie1 没有被找到。 - Kit
抱歉,没事了。我在一个非安全的http上测试,并将我的第一个cookie设置为“secure” :) - Kit
嗯,好的。根据这个页面,你需要为每个cookie发送'max-age''expires'。如果这不起作用,恐怕我已经没有更多的想法了。 :/ - aganders3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接