【问题标题】:Setting a Cookie on a browser using Python使用 Python 在浏览器上设置 Cookie
【发布时间】:2016-08-10 00:24:11
【问题描述】:

我正在处理一个项目。我想在用户登录时设置一个 cookie,以便记住他们。但是我已经尝试了互联网上的所有方法,从 Google 到 Stack Overflow。我似乎找不到有效的方法。

我试过了:

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'

令人惊讶的是,它也不起作用。我该怎么办?我该怎么做?我敢肯定我只是愚蠢而没有做正确的事情。

谢谢。

【问题讨论】:

    标签: python google-app-engine cookies


    【解决方案1】:

    你需要使用set_cookie() method:

    class Main(webapp2.RequestHandler):
        def get(self):
            self.response.set_cookie('lastvisit', 'test', max_age=360, path='/')
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-07
    • 2014-08-10
    • 2020-06-06
    • 1970-01-01
    • 2015-12-21
    • 2015-06-18
    • 2022-12-04
    • 2023-03-30
    相关资源
    最近更新 更多