Cookie Confusion - How to Set a Cookie

Discussion in 'Python' started by cbhoem@gmail.com, Apr 28, 2008.

  1. Guest

    Hi -

    I am trying my hand at python cookies. I'm confused about a few
    things though. Do the python cookies get written to a cookies text
    file? I have simple code below -- I see the cookie in my HTTP header
    but do not get anything in the cookie text file. I'm working on
    linux.

    print "Content-type: text/html"
    cookie = Cookie.SimpleCookie()
    cookie['Test'] = 'abc'
    print cookie
    print

    Are there rules about where in the header the set cookie line should
    be?

    Thanks in advance!
    Christian
     
    , Apr 28, 2008
    #1
    1. Advertising

  2. On Apr 28, 9:42 am, wrote:
    > ....I see the cookie in my HTTP header
    > but do not get anything in the cookie text file. I'm working on
    > linux.
    >
    > print "Content-type: text/html"
    > cookie = Cookie.SimpleCookie()
    > cookie['Test'] = 'abc'
    > print cookie
    > print
    >
    > Are there rules about where in the header the set cookie line should
    > be?


    Hi Christian. I think the cookie can go anywhere in
    the header, but I usually put it before the content-type.
    If you want to store the cookie to a file,
    or even better, to a database of some sort, you have to
    do it yourself, the Cookie module doesn't do it for you,
    I hope.

    # store cookie to /tmp/cookie.txt
    file("/tmp/cookie.txt","w").write(str(cookie))

    For parsing cookies, I stole and modified this from
    the Django source (for use in a cgi script):

    ===
    from Cookie import SimpleCookie
    import os

    # stolen and modified from Django
    def parse_cookie(cookie=None, environ=None):
    if cookie is None:
    if environ is None:
    environ = os.environ
    cookie = environ.get('HTTP_COOKIE', '')
    if cookie == '':
    return {}
    c = SimpleCookie()
    c.load(cookie)
    cookiedict = {}
    for key in c.keys():
    cookiedict[key] = c.get(key).value
    return cookiedict

    ===
    All the best. -- Aaron Watters

    ===
    http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=monster
     
    Aaron Watters, Apr 28, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 28, 1:37 pm, Aaron Watters <> wrote:
    > On Apr 28, 9:42 am, wrote:
    >
    > > ....I see the cookie in my HTTP header
    > > but do not get anything in the cookie text file. I'm working on
    > > linux.

    >
    > > print "Content-type: text/html"
    > > cookie = Cookie.SimpleCookie()
    > > cookie['Test'] = 'abc'
    > > print cookie
    > > print

    >
    > > Are there rules about where in the header the set cookie line should
    > > be?

    >
    > Hi Christian. I think the cookie can go anywhere in
    > the header, but I usually put it before the content-type.
    > If you want to store the cookie to a file,
    > or even better, to a database of some sort, you have to
    > do it yourself, the Cookie module doesn't do it for you,
    > I hope.
    >
    > # store cookie to /tmp/cookie.txt
    > file("/tmp/cookie.txt","w").write(str(cookie))
    >
    > For parsing cookies, I stole and modified this from
    > the Django source (for use in a cgi script):
    >
    > ===
    > from Cookie import SimpleCookie
    > import os
    >
    > # stolen and modified from Django
    > def parse_cookie(cookie=None, environ=None):
    > if cookie is None:
    > if environ is None:
    > environ = os.environ
    > cookie = environ.get('HTTP_COOKIE', '')
    > if cookie == '':
    > return {}
    > c = SimpleCookie()
    > c.load(cookie)
    > cookiedict = {}
    > for key in c.keys():
    > cookiedict[key] = c.get(key).value
    > return cookiedict
    >
    > ===
    > All the best. -- Aaron Watters
    >
    > ===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=monster



    Thanks for the code, Aaron. I will give it a try.

    I've been reading some more about cookielib and am not sure whether I
    should use Cookie or cookielib. This is what I want to do: a user is
    going to login. Upon a successful login, I want to write their name
    and date/time of visit to a cookie file. Which is the correct python
    module to use?
     
    , Apr 29, 2008
    #3

  4. > Thanks for the code, Aaron. I will give it a try.
    >
    > I've been reading some more about cookielib and am not sure whether I
    > should use Cookie or cookielib. This is what I want to do: a user is
    > going to login. Upon a successful login, I want to write their name
    > and date/time of visit to a cookie file. Which is the correct python
    > module to use?


    Cookie does parsing and generation of cookie strings
    for server-side applications like your CGI script.

    The cookielib module
    is designed for either implementing a client like a web browser
    or emulating a client/browser (for web scraping, for example).

    I think you want to use Cookie.
    The distinction could be made clearer in
    the docs, imho.

    Also, when you say "write the cookie file" I think you mean
    "store the cookie to the client browser". This should happen
    automatically when you send the cookie header to the client
    correctly (if the client is configured to cooperate).

    -- Aaron Watters

    ===
    http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=default does nothing
     
    Aaron Watters, Apr 29, 2008
    #4
  5. Guest

    On Apr 29, 3:35 pm, Aaron Watters <> wrote:
    > > Thanks for the code, Aaron. I will give it a try.

    >
    > > I've been reading some more about cookielib and am not sure whether I
    > > should use Cookie or cookielib. This is what I want to do: a user is
    > > going to login. Upon a successful login, I want to write their name
    > > and date/time of visit to a cookie file. Which is the correct python
    > > module to use?

    >
    > Cookie does parsing and generation of cookie strings
    > for server-side applications like your CGI script.
    >
    > The cookielib module
    > is designed for either implementing a client like a web browser
    > or emulating a client/browser (for web scraping, for example).
    >
    > I think you want to use Cookie.
    > The distinction could be made clearer in
    > the docs, imho.
    >
    > Also, when you say "write the cookie file" I think you mean
    > "store the cookie to the client browser". This should happen
    > automatically when you send the cookie header to the client
    > correctly (if the client is configured to cooperate).
    >
    > -- Aaron Watters
    >
    > ===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=default+does+n...



    Sorry for the slow replies. I've been in & out with a sick child.

    I'm used to my javascript cookies. They are automatically written to
    a cookie.txt file in a .mozilla dir under my user. When I say to
    'write the cookie file' this is what I was referring to. I was
    expecting my python cookie to automatically get written to the same
    file. I have't seen this happen yet.
     
    , May 1, 2008
    #5
  6. On May 1, 9:02 am, wrote:
    > On Apr 29, 3:35 pm, Aaron Watters <> wrote:
    >
    >
    >
    > > > Thanks for the code, Aaron. I will give it a try.

    >
    > > > I've been reading some more about cookielib and am not sure whether I
    > > > should use Cookie or cookielib. This is what I want to do: a user is
    > > > going to login. Upon a successful login, I want to write their name
    > > > and date/time of visit to a cookie file. Which is the correct python
    > > > module to use?

    >
    > > Cookie does parsing and generation of cookie strings
    > > for server-side applications like your CGI script.

    >
    > > The cookielib module
    > > is designed for either implementing a client like a web browser
    > > or emulating a client/browser (for web scraping, for example).

    >
    > > I think you want to use Cookie.
    > > The distinction could be made clearer in
    > > the docs, imho.

    >
    > > Also, when you say "write the cookie file" I think you mean
    > > "store the cookie to the client browser". This should happen
    > > automatically when you send the cookie header to the client
    > > correctly (if the client is configured to cooperate).

    >
    > > -- Aaron Watters

    >
    > > ===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=default+does+n...

    >
    > Sorry for the slow replies. I've been in & out with a sick child.
    >
    > I'm used to my javascript cookies. They are automatically written to
    > a cookie.txt file in a .mozilla dir under my user. When I say to
    > 'write the cookie file' this is what I was referring to. I was
    > expecting my python cookie to automatically get written to the same
    > file. I have't seen this happen yet.


    Hmmm. I don't know how cookies are stored on the client.
    I recommend using the standard cgi debug
    environment dump after setting
    a cookie to see if the cookie is coming back to the server.
    If it is not something is wrong -- you are setting it incorrectly,
    or the server or client is blocking the cookie.

    Note if you are testing on one machine: some browsers
    and servers have by default
    special security restrictions on
    "localhost" loopbacks -- it may be that this is causing
    either the server or the client to ignore the cookie.

    -- Aaron Watters
    ===
    http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=cooked
     
    Aaron Watters, May 2, 2008
    #6
    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. Replies:
    2
    Views:
    487
  2. middletree

    cookie confusion

    middletree, Jul 9, 2003, in forum: ASP General
    Replies:
    4
    Views:
    152
  3. Xeno Campanoli
    Replies:
    2
    Views:
    170
    Xeno Campanoli
    Jul 10, 2008
  4. Phil Powell
    Replies:
    2
    Views:
    219
    Grant Wagner
    Sep 5, 2003
  5. Charly Walter

    Cookie writing confusion

    Charly Walter, Apr 17, 2004, in forum: Javascript
    Replies:
    5
    Views:
    141
    Charly Walter
    Apr 20, 2004
Loading...

Share This Page