[Tutor] Requesting restricted URL (further authentication requested)

Sander Sweers sander.sweers at gmail.com
Tue Oct 19 23:02:02 CEST 2010

On 19 October 2010 21:56, Tim Johnson <tim at johnsons-web.com> wrote:
> I've written the following function which successfully gets an
> authenticated URL:
> def getRestrictedURL(authName,URL,log,pswd):
>        auth_handler = urllib2.HTTPBasicAuthHandler()
>        auth_handler.add_password(authName, URL,log,pswd)
>        opener = urllib2.build_opener(auth_handler)
>        urllib2.install_opener(opener)
>        return opener.open(URL).read()

The above should be:
    data = opener.open(URL).read() #does the initial authentication.
    return opener #return the opener object not the data you read.

> # But, alas, any links in content 'beneath' the URL
> # require additional authentication.

You are almost there. In order to authenticate initially you indeed
need to read the page. But then what you want is to return the opener
object, not the page you read which you do currently. The opener
object holds the authentication data and this opener object you will
need to use for every new url you want to read, opener.open().

Depending on what you want to do with the data you might want to split
this up into 2 functions. One that will authenticate and another that
read the url and returns the data. The second function would need to
be passed the opener object which the first one returned.


More information about the Tutor mailing list