簡體   English   中英

如何使用CherryPy設置多個Cookie

[英]How do I set multiple cookies with CherryPy

從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?

我認為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' )。 現在應該修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM