http request with cookie sending

Discussion in 'Python' started by itay_k, Apr 18, 2006.

  1. itay_k

    itay_k Guest

    Hi,

    I want to send a cookie on some http request (with urllib2),
    so I created a Cookie but I cant associate it with CookieJar object.

    for example:

    import Cookie
    import cookielib, urllib2

    C = Cookie.SimpleCookie()
    C["a"] = "b"

    cj = cookielib.CookieJar()
    cj.set_cookie(C)

    in the last line I got:
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "C:\Python24\lib\cookielib.py", line 1593, in set_cookie
    if cookie.domain not in c: c[cookie.domain] = {}
    AttributeError: 'SimpleCookie' object has no attribute 'domain'

    why?
    maybe.. there is any code example for sending cookie with some http
    request operation?

    thanks alot,
    Itay.
    itay_k, Apr 18, 2006
    #1
    1. Advertising

  2. itay_k

    Kent Johnson Guest

    itay_k wrote:
    > Hi,
    >
    > I want to send a cookie on some http request (with urllib2),
    > so I created a Cookie but I cant associate it with CookieJar object.


    You have to use a cookielib.Cookie, not Cookie.SimpleCookie():

    import cookielib, urllib2

    cj = cookielib.CookieJar()
    cookie = cookielib.Cookie(...your cookie data here...)
    cj.set_cookie(cookie)

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)

    data = urllib2.urlopen(...).read()

    Kent
    Kent Johnson, Apr 19, 2006
    #2
    1. Advertising

  3. itay_k

    John J. Lee Guest

    Kent Johnson <> writes:

    > itay_k wrote:
    > > Hi,
    > >
    > > I want to send a cookie on some http request (with urllib2),
    > > so I created a Cookie but I cant associate it with CookieJar object.

    >
    > You have to use a cookielib.Cookie, not Cookie.SimpleCookie():


    As I mention in the other thread Itay started, he probably doesn't
    want to do that either.


    John
    John J. Lee, Apr 20, 2006
    #3
    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. ad
    Replies:
    2
    Views:
    7,443
    Kevin Spencer
    Jan 27, 2006
  2. nRk
    Replies:
    1
    Views:
    970
    Steven D'Aprano
    Feb 12, 2009
  3. Francis Hwang
    Replies:
    2
    Views:
    98
    Francis Hwang
    Dec 5, 2004
  4. Paul  Wolpe
    Replies:
    0
    Views:
    149
    Paul Wolpe
    Oct 17, 2005
  5. Richard  Lawrence

    HTTP::Cookie won't store sent cookie

    Richard Lawrence, Mar 17, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    178
    Richard Lawrence
    Mar 21, 2005
Loading...

Share This Page