Getting and Setting Cookies

Discussion in 'Python' started by Vlad Dogaru, Jul 18, 2006.

  1. Vlad Dogaru

    Vlad Dogaru Guest

    Hello,

    I am trying to use cookies and Python to create a simple login example.
    But I am very disoriented at the existence of two cookie libraries,
    namely Cookie and cookielib. I have seen examples of setting cookies
    (although I am still not sure about timestamps and cookie lifespan),
    but no reference to getting the currently set cookies. For instance, I
    want to see if there is any 'user' value, to check whether the user has
    logged in. Please, enlighten me.

    Thanks in advance,
    Vlad
    Vlad Dogaru, Jul 18, 2006
    #1
    1. Advertising

  2. Vlad Dogaru

    John J. Lee Guest

    "Vlad Dogaru" <> writes:

    > I am trying to use cookies and Python to create a simple login example.
    > But I am very disoriented at the existence of two cookie libraries,
    > namely Cookie and cookielib. I have seen examples of setting cookies

    [...]

    From the cookielib docs:

    http://docs.python.org/lib/module-cookielib.html

    | The cookielib module defines classes for automatic handling of HTTP
    | cookies. It is useful for accessing web sites that require small
    | pieces of data - cookies - to be set on the client machine by an HTTP
    | response from a web server, and then returned to the server in later
    | HTTP requests.

    (note the *accessing* there)

    [...]

    | Module Cookie: HTTP cookie classes, principally useful for server-side
    | code. The cookielib and Cookie modules do not depend on each
    | other.


    Module cookielib is for web client code (writing code that works like
    a browser). Module Cookie is for server-side code (writing code to
    make a web site work). You don't make it entirely clear which you're
    doing, but it sounds like the latter.


    John
    John J. Lee, Jul 19, 2006
    #2
    1. Advertising

  3. Vlad Dogaru

    Vlad Dogaru Guest

    John J. Lee wrote:
    > "Vlad Dogaru" <> writes:
    >
    > > I am trying to use cookies and Python to create a simple login example.
    > > But I am very disoriented at the existence of two cookie libraries,
    > > namely Cookie and cookielib. I have seen examples of setting cookies

    > [...]
    >
    > From the cookielib docs:
    >
    > http://docs.python.org/lib/module-cookielib.html
    >
    > | The cookielib module defines classes for automatic handling of HTTP
    > | cookies. It is useful for accessing web sites that require small
    > | pieces of data - cookies - to be set on the client machine by an HTTP
    > | response from a web server, and then returned to the server in later
    > | HTTP requests.
    >
    > (note the *accessing* there)
    >
    > [...]
    >
    > | Module Cookie: HTTP cookie classes, principally useful for server-side
    > | code. The cookielib and Cookie modules do not depend on each
    > | other.
    >
    >
    > Module cookielib is for web client code (writing code that works like
    > a browser). Module Cookie is for server-side code (writing code to
    > make a web site work). You don't make it entirely clear which you're
    > doing, but it sounds like the latter.


    I am trying to write a simple login script. I understand (or rather I
    think I understand) how to set a cookie with the Cookie module. My
    problem is getting the cookies that are currently set. How can I do
    that?
    Vlad Dogaru, Jul 19, 2006
    #3
  4. Vlad Dogaru

    John J. Lee Guest

    "Vlad Dogaru" <> writes:
    [...]
    > I am trying to write a simple login script. I understand (or rather I
    > think I understand) how to set a cookie with the Cookie module. My
    > problem is getting the cookies that are currently set. How can I do
    > that?


    You still haven't explicitly said that you're writing server-side
    code. If you are:

    IIRC, you .load() it from the HTTP header value, then you use it as a
    mapping (though, oddly, that seems undocumented, except in the
    "Example" section of the docs). A working CGI-based example (written
    in a rather retro style for ease of deployment):

    http://codespeak.net/svn/wwwsearch/ClientCookie/trunk/cookietest.cgi


    If you want to iterate over all cookies using module Cookie (that
    script does not), use .keys(), .values() or .items() on your
    SimpleCookie instance (it's also a little odd that instances of class
    SimpleCookie do not represent a single cookie, but a collection of
    cookies).


    John
    John J. Lee, Jul 19, 2006
    #4
  5. Vlad Dogaru

    Vlad Dogaru Guest

    John J. Lee wrote:
    > "Vlad Dogaru" <> writes:
    > [...]
    > > I am trying to write a simple login script. I understand (or rather I
    > > think I understand) how to set a cookie with the Cookie module. My
    > > problem is getting the cookies that are currently set. How can I do
    > > that?

    >
    > You still haven't explicitly said that you're writing server-side
    > code. If you are:
    >
    > IIRC, you .load() it from the HTTP header value, then you use it as a
    > mapping (though, oddly, that seems undocumented, except in the
    > "Example" section of the docs). A working CGI-based example (written
    > in a rather retro style for ease of deployment):
    >
    > http://codespeak.net/svn/wwwsearch/ClientCookie/trunk/cookietest.cgi
    >
    >
    > If you want to iterate over all cookies using module Cookie (that
    > script does not), use .keys(), .values() or .items() on your
    > SimpleCookie instance (it's also a little odd that instances of class
    > SimpleCookie do not represent a single cookie, but a collection of
    > cookies).


    That's pretty much what I was trying to find out. Thanks for the
    pointer.

    Vlad
    Vlad Dogaru, Jul 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,452
    Hans Kesting
    Feb 3, 2005
  2. archana
    Replies:
    1
    Views:
    498
  3. _Who
    Replies:
    7
    Views:
    2,630
  4. techie
    Replies:
    3
    Views:
    147
    Curt_C [MVP]
    Sep 17, 2003
  5. Mike
    Replies:
    1
    Views:
    97
    Qu├ębec
    Sep 4, 2004
Loading...

Share This Page