http request with cookie sending

I

itay_k

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.
 
K

Kent Johnson

itay_k said:
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
 
J

John J. Lee

Kent Johnson said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top