mod_python & Cookie - unexpected dictionary value

Discussion in 'Python' started by Jan Danielsson, Apr 13, 2007.

  1. Hello all,

    I'm sure I'm not using this right, but I don't understand what I'm
    doing wrong. What I want is to get all the cookies from the request,
    then extract the 'sessId' cookie. I'm using this code:

    -----------------
    from mod_python import Cookie

    [---]

    def index(req, sessId = None):

    cookies = Cookie.get_cookies(req)

    if not sessId and cookies.has_key('sessId'):
    sessId = cookies['sessId']

    sess = Session(req, sessId)

    httphdr(req)
    -----------------

    (Note: The Session() constructor will attempt to set the cookie, and
    httphdr() is responsible for the send_http_header() call).

    This is the part I don't understand. If the sessId cookie exists, it
    will fail because the returned sessId object will be a Cookie object. If
    I print str(sessId), I will get the output "sessId=blah". The cookie is
    set using:

    c = Cookie.Cookie('sessId', 'blah')
    c.expires = time.time() + 60*30
    Cookie.add_cookie(req, c)

    So, the part I don't understand is why

    sessId = cookies['sessId']

    ... is returning a Cookie-object. I would have expected to get a
    string containing "blah"?

    I can get this to work, by doing this:


    cookies = Cookie.get_cookies(req)
    c = str(cookies['sessId']).split('=', 1)

    ...then use c[1]. Is that the proper way? Seems kind of strange to
    store the cookies in a dictonary without being able to use the benefits
    of them?


    --
    Kind regards,
    Jan Danielsson
    ------------ And now a word from our sponsor ------------------
    Want to have instant messaging, and chat rooms, and discussion
    groups for your local users or business, you need dbabble!
    -- See http://netwinsite.com/sponsor/sponsor_dbabble.htm ----
     
    Jan Danielsson, Apr 13, 2007
    #1
    1. Advertising

  2. Jan Danielsson wrote:
    [---]

    Never mind. Cookie objects have a "value" attribute -- that's what I
    was doing wrong.

    --
    Kind regards,
    Jan Danielsson
    ------------ And now a word from our sponsor ------------------
    Want to have instant messaging, and chat rooms, and discussion
    groups for your local users or business, you need dbabble!
    -- See http://netwinsite.com/sponsor/sponsor_dbabble.htm ----
     
    Jan Danielsson, Apr 14, 2007
    #2
    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. Ilias Lazaridis
    Replies:
    6
    Views:
    446
    Ilias Lazaridis
    Feb 21, 2006
  2. blbmdsmith
    Replies:
    1
    Views:
    654
    Graham Dumpleton
    Dec 14, 2006
  3. james_027
    Replies:
    1
    Views:
    330
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Rob Clewley
    Replies:
    3
    Views:
    235
    Rob Clewley
    Jan 13, 2009
  5. Navkirat Singh
    Replies:
    6
    Views:
    3,089
    Navkirat Singh
    Jul 29, 2010
Loading...

Share This Page