【问题标题】:setting and reading cookies in mod_python (apache)在 mod_python (apache) 中设置和读取 cookie
【发布时间】:2012-07-05 18:10:00
【问题描述】:

我已经看到很多解释如何读写 cookie 的东西,但是,我不知道如何在 apache 的 mod_python 中做到这一点。我试着把它放在我的 HTML 代码的开头,但它说把它放在 HTTP 标头中。我怎么做? 另外,我该如何找回它们?我最初主要看这个网站: http://webpython.codepoint.net/cgi_set_the_cookie

我的代码目前是这样开始的(它显示为 HTML 的一部分)

Content-Type: text/html
Set-Cookie: test=1
<html>
    <head>

【问题讨论】:

    标签: python apache cookies session-cookies mod-python


    【解决方案1】:

    mod_python 不是 CGI,它提供了自己的方式来设置和读取 cookie:

    from mod_python import Cookie, apache
    import time
    
    def handler(req):
        # read a cookie
        spam_cookie = get_cookie(req, 'spam')
    
        # set a cookie
        egg_cookie = Cookie.Cookie('eggs', 'spam')
        egg_cookie.expires = time.time() + 300
        Cookie.add_cookie(req, egg_cookie)
    
        req.write('<html><head></head><body>There's a cookie</body></html>')
        return apache.OK
    

    您可以在此处找到更多文档:http://www.modpython.org/live/current/doc-html/pyapi-cookie.html

    【讨论】:

    • 只是一个问题:这是您必须维护的遗留应用程序吗?否则 - 如果它是用于一个新项目并且您有任何选择,我强烈建议您改用 mod_wsgi。
    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多