urllib.urlopen() with pages that requires cookies.

John J. Lee jjl at pobox.com
Sat Apr 29 15:28:01 EDT 2006


"mwt" <michaeltaft at gmail.com> writes:

> Fredrik Lundh wrote:
[...]
> > use urllib2 and cookielib.  here's an outline:
> >
> >     import urllib2, cookielib
> >
> >     # set things up
> >     jar = cookielib.CookieJar()
> >     handler = urllib2.HTTPCookieProcessor(jar)
> >     opener = urllib2.build_opener(handler)
> >     urllib2.install_opener(opener)
> >
> >     data = urllib2.urlopen(someurl).read()
> >
> >     # ...
> >
> >     data = urllib2.urlopen(someotherurl).read()
> >
> >     # ...
> >
> >     # dump cookie jar contents (for debugging)
> >     for cookie in jar:
> >         print cookie
> >
> > </F>
> 
> How would this outline be changed/expanded if it also needed to handle
> basic authentication?

...
handler = urllib2.HTTPCookieProcessor(jar)
pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
# use None for realm: send username and password for all realms
pm.add_password(None, "http://example.com/", "joe", "password")
auth_handler = HTTPBasicAuthHandler(pm)
opener = urllib2.build_opener(handler, auth_handler)
...


Note that (I just noticed that) using both basic auth and digest auth
handlers at the same time is broken, at least in SVN :-((


John




More information about the Python-list mailing list