1

这是我的代码:

import webapp2
class Index(webapp2.RequestHandler):
    def get_cookie(self, name, notfound=""):
        return self.request.cookies.get(name, notfound)
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers.add_header('Set-Cookie', "Foo=Hello World!")
        self.response.headers.add_header('Set-Cookie', "Bar=With, Commas")

        txt1 = self.get_cookie("Foo", "Foo cookie not found!")
        txt2 = self.get_cookie("Bar", "Bar cookie not found!")

        self.response.out.write(txt1 + " " + txt2)

app = webapp2.WSGIApplication([('/', Index)], debug=True)

代码会输出,但是输出有问题...

在此处输入图像描述

浏览器甚至不转义空格......我应该怎么做才能正确显示 cookie 值?

4

1 回答 1

4

您想改用响应.set_cookie()方法

def get(self):
    self.response.headers['Content-Type'] = 'text/plain'

    self.response.set_cookie('Foo', 'Hello World!)
    self.response.set_cookie('Foo', 'With, Commas')

它负责适当的转义等。

于 2013-02-14T18:41:15.067 回答