使用Python在浏览器上设置Cookie

使用Python在浏览器上设置Cookie,python,google-app-engine,cookies,Python,Google App Engine,Cookies,我正在做一个项目。我想在用户登录时设置一个cookie,以便记住他们。然而,我已经尝试了互联网上的每一种方法,从谷歌到堆栈溢出。我似乎找不到一个有效的方法 我试过: class Main(webapp2.RequestHandler): def get(self): print "Set-Cookie: testing=test123cookie" print "Content-Type: text/html\n" 但这不起作用。然后尝试: import

我正在做一个项目。我想在用户登录时设置一个cookie,以便记住他们。然而,我已经尝试了互联网上的每一种方法,从谷歌到堆栈溢出。我似乎找不到一个有效的方法

我试过:

class Main(webapp2.RequestHandler):
    def get(self):
        print "Set-Cookie: testing=test123cookie"
        print "Content-Type: text/html\n"
但这不起作用。然后尝试:

import Cookie

class Main(webapp2.RequestHandler):
    def get(self):
        cookie = Cookie.SimpleCookie()
        cookie['lastvisit'] = "test"
        print cookie
        print 'Content-Type: text/html\n'
令人惊讶的是,它也不起作用。我该怎么办?我该怎么做?我确信我只是在做傻事,没有做正确的事情

谢谢。

您需要使用:


要检索cookie,请执行以下操作:
self.request.cookies.get(“lastvisit”)
(请参阅)。
class Main(webapp2.RequestHandler):
    def get(self):
        self.response.set_cookie('lastvisit', 'test', max_age=360, path='/')