Pregunta

I've seen plenty of things explaining how to read and write cookies, however, I have no clue about how to do it in mod_python in apache. I tried putting this at the start of my HTML code, but it says to put it in the HTTP header. How do I do that? Also, how do I retrieve them? I was originally looking mainly at this site: http://webpython.codepoint.net/cgi_set_the_cookie

My code currently starts like this (and it displays as part of the HTML)

Content-Type: text/html
Set-Cookie: test=1
<html>
    <head>
¿Fue útil?

Solución

mod_python is not CGI, and provides it's own way to set and read cookies:

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

You'll find more documentation here : http://www.modpython.org/live/current/doc-html/pyapi-cookie.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top