HTTP - basic authentication example.

Jaime Wyant programmer.py at gmail.com
Fri Sep 17 14:37:44 CEST 2004


Good stuff.  I knew if we knocked our heads together something good
would come out ;-).

jw


On Fri, 17 Sep 2004 13:20:23 +0100, Michael Foord <fuzzyman at gmail.com> wrote:
> I've added this example to the python cookbook (which basically does
> what your example does I think) -
> 
> The proper way of actually doing this is to use an
> HTTPBasicAuthHandler along with an HTTPPasswordMgr.
> The python documentation on this is actually pretty minimal - so below
> is an example showing how to do this.
> The main problem with HTTPPasswordMgr is that you must *already* know
> the realm - so we're going to use the HTTPPasswordMgrWithDefaultRealm
> instead !!
> 
> theurl = 'http://www.someserver.com/highestlevelprotectedpath/somepage.htm'
> username = 'johnny'
> password = 'XXXXXX'         # a great password
> 
> passman = urllib2.HTTPPasswordMgrWithDefaultRealm()      # this
> creates a password manager
> passman.add_password(None, theurl, username, password)      # because
> we have put None at the start it will always use this
> username/password combination
> 
> authhandler = urllib2.HTTPBasicAuthHandler(passman)                 #
> create the AuthHandler
> 
> opener = urllib2.build_opener(authhandler)
>      # build an 'opener' using the handler we've created
> # you can use the opener directly to open URLs
> # *or* you can install it as the default opener so that all calls to
> urllib2.urlopen use this opener
> urllib2.install_opener(opener)
> 
> # All calls to urllib2.urlopen will now use our handler
> 
> 
> 
> 
> On Wed, 15 Sep 2004 11:21:21 -0500, Jaime Wyant <programmer.py at gmail.com> wrote:
> > FWIW, this is how I handle Basic Authentication:
> >
> > import urllib2
> > import sys
> >
> > class AuthenticateAllURIs:
> >     """This class authenticates all Basic Authentication using uname
> > / pword."""
> >    def __init__(self,uname,pword):
> >        self.uname = uname
> >        self.pword = pword
> >
> >    def find_user_password(self, realm, host):
> >        # Note, that this class doesn't take `realm' into consideration.
> >        return self.uname, self.pword
> >
> >    def add_password( self, realm, uri, user, password ):
> >        pass
> >
> > auth = urllib2.ProxyBasicAuthHandler(AuthenticateAllURIs('umjaw', 'fuse3'))
> > opener = urllib2.build_opener( auth )
> > urllib2.install_opener( opener )
> > wp = urllib2.urlopen("http://www.slashdot.org")
> > print wp.read()
> >
> > HTH,
> > jw
> >
> > On 15 Sep 2004 08:37:12 -0700, Michael Foord <fuzzyman at gmail.com> wrote:
> > [ snip! ]
> >
> 
> --
> http://www.Voidspace.org.uk
> The Place where headspace meets cyberspace. Online resource site -
> covering science, technology, computing, cyberpunk, psychology,
> spirituality, fiction and more.
> 
> ---
> http://www.Voidspace.org.uk/atlantibots/pythonutils.html
> Python utilities, modules and apps.
> Including Nanagram, Dirwatcher and more.
> ---
> http://www.fuchsiashockz.co.uk
> http://groups.yahoo.com/group/void-shockz
> ---
> 
> Everyone has talent. What is rare is the courage to follow talent
>  to the dark place where it leads. -Erica Jong
> Ambition is a poor excuse for not having sense enough to be lazy.
>          -Milan Kundera
>



More information about the Python-list mailing list