[Tutor] Cookies and authorization - urllib2 vs urllib

Kent Johnson kent37 at tds.net
Fri Jun 24 04:06:29 CEST 2005


D. Hartley wrote:
> P.S. Kent - thank you for the helpful tutorials on authentication,
> they really cleared up the process a great deal.  The only problem is:
> 
> When I create an opener to process the cookies, it looks like this:
> 
> opener = 
> urllib2.build_opener(urllib2.HTTPCookieProcessor(myjar))
> 
> ..where myjar is cookielib.CookieJar()
> 
> But in the examples for authentication, when I create the opener:
> 
> opener = urllib2.build_opener(authhandler)
> 
> ..where authhandler is urllib2.HTTPBasicAuthHandler(passwordmanager)
> 
> So both use build_opener, but the thing I pass in, from what I am
> looking at so far, has to be a cookie processor OR an authenticator. 
> How can I do both at once?

I think build_opener() can take multiple arguments:
cookieHandler = urllib2.HTTPCookieProcessor(myjar)
authhandler = urllib2.HTTPBasicAuthHandler(passwordmanager)
opener = urllib2.build_opener(cookieHandler, authhandler)

Kent



More information about the Tutor mailing list