[Tutor] getting a webpage via python

Paul Tremblay phthenry at iglou.com
Tue Mar 8 20:38:09 CET 2005

On Tue, Mar 08, 2005 at 01:42:40PM -0500, Kent Johnson wrote:
> Date: Tue, 08 Mar 2005 13:42:40 -0500
> From: Kent Johnson <kent37 at tds.net>
> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
> Cc: python tutor <tutor at python.org>
> Subject: Re: [Tutor] getting a webpage via python
> Paul Tremblay wrote:
> >This is giving me 401 error, authorization required. Here's what I have:
> >
> >
> >auth_handler = urllib2.HTTPBasicAuthHandler()
> >auth_handler.add_password('realm', '', 'myname', 'password')
> >opener = urllib2.build_opener(auth_handler)
> ># ...and install it globally so it can be used with urlopen.
> >urllib2.install_opener(opener)
> >print urllib2.urlopen('http://nnn.nnn.n.n').read()
> >
> >This is almost the literal code, except I changed my log in name and
> >passowrd.
> >
> >What am I supposed to put for realm? I checked the docs on line, and it
> >doens't mention what realm is.
> The realm name is part of the web server authentication configuration. If 
> you browse to the web page in your browser it will show the realm in the 
> auth dialog. For example my browser (Firefox) shows
> Enter user name and password for "Curriculum Builder" at http://localhost
> The realm name is "Curriculum Builder"

Thanks. I got it to work.

I tired to open the page in Koqueror web browser and got this message:

Authorization Dialog

You need to supply a username and password to access this site.

Site: WRT54G at nnn.nnn.n.n

I tried useing "Authorizaiton Dialog" with no success. So I tried opening
the same page with lynx with the trace option on. I found that lynx used
WRT54G as the realm, so I tried it and it worked.

> >Also, I assume I am supposed to use the loopback address when I set up
> >the handler?
> No, use your server name, in your example nnn.nn.n.n
> >Also, I know I am going to have another question once I solve this one,
> >and since it is directly related, I'll ask it now. How can I use a
> >password system that is not hardcoded in my text? Should I just setup a
> >password file? 
> Yes, something like that. You will have to store the password in plaintext 
> (or a reversible cipher) since that is what HTTPBasicAuthHandler needs.

So I just make a file called /etc/router_passwords  and include
something like 

WRT54G username password

Then parse the file, and supply the info to the password handler? This
is easy to do, and I guess it is secure. I am networking on a home
system, so security is not so big a concern. However, I have seen a lot
of people struggle with getting router addresses from the WRT54G,so I
thought I might offer my script, and I wanted to do things a secure,
normal way.




*Paul Tremblay         *
*phthenry at iglou.com    *

More information about the Tutor mailing list