设置和读取mod峎python(apache)中的cookie

2024-06-07 00:59:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

Tags: the代码modhttpnet网站apachehtml
1条回答
网友
1楼 · 发布于 2024-06-07 00:59:12

mod\u 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

相关问题 更多 >