send cookie on request with urllib2

John J. Lee jjl at pobox.com
Thu Apr 20 15:13:27 EDT 2006


"itay_k" <itayyy at gmail.com> writes:

> Hi,
> 
> I dont understand why this is so complicated, just to add one line of
> cookie header on the GET request.

You haven't said what you're really trying to do.

http://www.catb.org/~esr/faqs/smart-questions.html#goal


> This is my unworking code:
> import time
> import Cookie
> import cookielib, urllib2
> 
> c= cookielib.Cookie(1,"Name","Tom", 80,False, "itay", False, False,
> "d:\\asddd",False, False,time.time()+1000,False,None,None,None)

Constructing your own Cookie instances is rarely necessary or
sensible.  If you do, I recommend getting the constructor arguments by
inspecting the Cookie object the server actually returns, rather than
using the values you *think* you know are correct.


> cj = cookielib.CookieJar()
> cj.set_cookie(c)
> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
> opener.open(r'http://itay/temp.html")
> 
> why this isn't working?

Please define "working".

If you want to handle cookies when opening URLs, just do this:

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
opener.open("http://itay/temp.html")
# more .open() calls go here...


Of course, on the first HTTP request, cookie handling cannot have any
effect at all unless you've somehow loaded some old cookies first.
Your .open() may only involve a single HTTP request (though
server-side software may detect the absence of a cookie on the first
request, and do a page refresh or redirect so it can see its cookie
returned to it again; vanilla urllib2 handles redirects but not
refreshes; package ClientCookie handles the latter, amongst other
things).

Regardless, turning on cookielib and httplib debug output will likely
be helpful if you're stuck (if only to post the output to this
newsgroup):

import logging, urllib2, sys

hh = urllib2.HTTPHandler()
hsh = urllib2.HTTPSHandler()
hh.set_http_debuglevel(1)
hsh.set_http_debuglevel(1)
opener = urllib2.build_opener(hh, hsh, urllib2.HTTPCookieProcessor())
logger = logging.getLogger("cookielib")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.DEBUG)

response = opener.open("http://wwwsearch.sf.net/cgi-bin/cookietest.cgi")


John




More information about the Python-list mailing list