Re: urllib.urlopen() with pages that requires cookies.
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: urllib.urlopen() with pages that requires cookies.

From: John J. Lee <jjl@pobox.com>
Date: Sat Apr 29 2006 - 21:28:01 CEST

"mwt" <michaeltaft@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
Received on Mon May 1 00:44:50 2006