Setting expirty data on a cookie

Discussion in 'Python' started by sophie_newbie, Apr 22, 2008.

  1. Does anyone know how to do this? I can't seem to make it work.

    I'm using:

    c = Cookie.SimpleCookie()
    c['data'] = "unamepwordwhatever"
    c.expires = time.time() + 300
    print c


    This doesn't seem to work, so I'm assuming isn't the correct way to
    set an expiry data? Anyone able to help me out here?

    Thanks!
     
    sophie_newbie, Apr 22, 2008
    #1
    1. Advertising

  2. sophie_newbie

    David Guest

    On Tue, Apr 22, 2008 at 6:21 PM, sophie_newbie <> wrote:
    > Does anyone know how to do this? I can't seem to make it work.
    >
    > I'm using:
    >
    > c = Cookie.SimpleCookie()
    > c['data'] = "unamepwordwhatever"
    > c.expires = time.time() + 300
    > print c
    >
    >
    > This doesn't seem to work, so I'm assuming isn't the correct way to
    > set an expiry data? Anyone able to help me out here?
    >


    You're probably looking for cookielib.Cookie
     
    David, Apr 22, 2008
    #2
    1. Advertising

  3. On Apr 22, 8:38 pm, David <> wrote:
    > On Tue, Apr 22, 2008 at 6:21 PM,sophie_newbie<> wrote:
    > > Does anyone know how to do this? I can't seem to make it work.

    >
    > > I'm using:

    >
    > > c = Cookie.SimpleCookie()
    > > c['data'] = "unamepwordwhatever"
    > > c.expires = time.time() + 300
    > > print c

    >
    > > This doesn't seem to work, so I'm assuming isn't the correct way to
    > > set an expiry data? Anyone able to help me out here?

    >
    > You're probably looking for cookielib.Cookie


    I don't think so, to give you a more complete picture, if I run this
    code:


    import Cookie
    import time
    c = Cookie.SimpleCookie()
    c['data'] = "unamepwordwhatever"
    c.expires = time.time() + 300
    print c


    This codes gives an output of:

    "Set-Cookie: data=unamepwordwhatever"

    As in there is no mention of an expiry date, when surely there should
    be?

    Thanks for any advice.
     
    sophie_newbie, Apr 24, 2008
    #3
  4. On Apr 24, 12:41 pm, sophie_newbie <> wrote:
    > On Apr 22, 8:38 pm, David <> wrote:
    >
    > > On Tue, Apr 22, 2008 at 6:21 PM,sophie_newbie<> wrote:
    > > > Does anyone know how to do this? I can't seem to make it work.

    >
    > > > I'm using:

    >
    > > > c = Cookie.SimpleCookie()
    > > > c['data'] = "unamepwordwhatever"
    > > > c.expires = time.time() + 300
    > > > print c

    >
    > > > This doesn't seem to work, so I'm assuming isn't the correct way to
    > > > set an expiry data? Anyone able to help me out here?

    >
    > > You're probably looking for cookielib.Cookie

    >
    > I don't think so, to give you a more complete picture, if I run this
    > code:
    >
    > import Cookie
    > import time
    > c = Cookie.SimpleCookie()
    > c['data'] = "unamepwordwhatever"
    > c.expires = time.time() + 300
    > print c
    >
    > This codes gives an output of:
    >
    > "Set-Cookie: data=unamepwordwhatever"
    >
    > As in there is no mention of an expiry date, when surely there should
    > be?
    >
    > Thanks for any advice.


    Ok this seems to work:

    import Cookie
    import time
    c = Cookie.SimpleCookie()
    c['data'] = "unamepwordwhatever"
    c['data']['expires'] = 30 * 24 * 60 * 60
    print c

    Gives an output of:

    "Set-Cookie: data=unamepwordwhatever; expires=Sat, 24-May-2008
    12:11:36 GMT"

    Bizarre that this information was so hard to find!
     
    sophie_newbie, Apr 24, 2008
    #4
  5. sophie_newbie

    David Guest

    > import Cookie
    > import time
    > c = Cookie.SimpleCookie()
    > c['data'] = "unamepwordwhatever"
    > c['data']['expires'] = 30 * 24 * 60 * 60
    > print c
    >
    > Gives an output of:
    >
    > "Set-Cookie: data=unamepwordwhatever; expires=Sat, 24-May-2008
    > 12:11:36 GMT"
    >


    Hi again. I didn't see your replies until now.

    Disclaimer: I've never worked with cookies before, I'm just going by
    the rfc, python docs, and wikipedia.

    I think the confusion exists because the Cookie module has an unusual
    definition of cookies. What we call cookies (key + value +
    attributes), the Cookie module calls a Morsel. What the Cookie module
    calls a cookie is in fact the collection of Set-Cookie headers that
    will be sent by the server.

    So for code like this:

    c = Cookie.SimpleCookie()
    c['data1'] = 123
    c['data2'] = 456

    the server will output 2 cookies like this:

    Set-Cookie: data1=123
    Set-Cookie: data2=456

    This is why when you want to set the expiry date for one of the
    cookies, you need syntax like this:

    c['data2']['expires'] = 30 * 24 * 60 * 60

    Another note: 'expires' is apprantly a legacy attribute for early
    Netscape browsers. The RFC and python source comments suggest that you
    use 'Max-Age' instead.

    I think that the Cookie module author wanted to represent http state
    as a python dictionary, but chose an unfortunate name for the class.
    Also, the example page doesn't go into detail about setting
    attributes.

    David.
     
    David, Apr 25, 2008
    #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. Ben
    Replies:
    3
    Views:
    5,856
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    574
  3. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,485
    Bruce Barker
    Jan 10, 2006
  4. Vikram Bhatia
    Replies:
    1
    Views:
    237
    kaeli
    Oct 11, 2003
  5. Timur Tabi
    Replies:
    1
    Views:
    126
    Timur Tabi
    May 14, 2004
Loading...

Share This Page