Cookie.py troubles.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Hearsum

    Cookie.py troubles.

    I'm confused about the way Cookie.load outputs cookies. Is there a way I
    can tell it to give me the value of a key?

    I set my cookie like this:

    mycookie = Cookie.SimpleCo okie()
    mycookie['key'] = "foobar"

    print mycookie
    print("Content-Type: text/html\n\n")
    print("meow")

    Now, to retrieve it, the documentation says I parse the HTTP_COOKIE
    environment variable. Makes sense to me. But when I print it out I get
    "Set-Cookie: key=foobar;". I was thinking of parsing it with cgi.parse_qs,
    but even if i do print(mycookie. output(header=" ")) there's still a ; at
    the end of the cookie.

    Here's what I use to print them out:

    cookie = Cookie.SimpleCo okie()
    cookie.load(os. environ['HTTP_COOKIE'])

    print("Content-Type: text/html\n\n")
    print(mycookie. output(header=" "))

    Output:

    key=foobar;


    Can anyone provide any advice?

    - Ben Hearsum
  • John J. Lee

    #2
    Re: Cookie.py troubles.

    Ben Hearsum <bhearsum@myrea lbox.com> writes:
    [...][color=blue]
    > I set my cookie like this:
    >
    > mycookie = Cookie.SimpleCo okie()
    > mycookie['key'] = "foobar"
    >
    > print mycookie
    > print("Content-Type: text/html\n\n")
    > print("meow")
    >
    > Now, to retrieve it, the documentation says I parse the HTTP_COOKIE
    > environment variable. Makes sense to me. But when I print it out I get
    > "Set-Cookie: key=foobar;". I was thinking of parsing it with cgi.parse_qs,
    > but even if i do print(mycookie. output(header=" ")) there's still a ; at
    > the end of the cookie.[/color]

    Why do you want to parse Set-Cookie if you're writing a CGI script?

    Set-Cookie is what the server sends to the client. HTTP_COOKIE is set
    by the server, not by Python, to the value than was sent by the client
    to the server. I guess SimpleCookie has a __str__ method that makes
    it print out Set-Cookie: key=foobar; -- seems like useful behaviour.
    [color=blue]
    > Here's what I use to print them out:
    >
    > cookie = Cookie.SimpleCo okie()
    > cookie.load(os. environ['HTTP_COOKIE'])[/color]

    Yep, in comes the Cookie: header, I presume SimpleCookie parses it,
    and then...

    [color=blue]
    > print("Content-Type: text/html\n\n")
    > print(mycookie. output(header=" "))[/color]

    You send the cookie back to the client again.

    [color=blue]
    > Output:
    >
    > key=foobar;[/color]

    This seems to contradict what you said before ("key=foobar ;"
    vs. "Set-Cookie: key=foobar;").



    John

    Comment

    Working...