Re: HTTP - basic authentication example.

From: Jaime Wyant (programmer.py_at_gmail.com)
Date: 09/17/04


Date: Fri, 17 Sep 2004 07:37:44 -0500
To: python-list@python.org

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@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@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@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
>



Relevant Pages

  • Re: Evaluate coding and style
    ... Once again this is really my first time using python. ... $ ./homedir_exists.py root mqm pcap ... root successful! ... instead of using re, I would split each line into it's component fields, then just match the username fields against the usernames passed in... ...
    (comp.lang.python)
  • Re: eclipse pywintypes.com_error
    ... line 98, in _GetGoodDispatchAndUserName ... return, userName) ... IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, ... am using python for subversion hook scripts written in python for AD ...
    (comp.lang.python)
  • Python HTTP digest authentication woes...
    ... I'm trying to access the XML version of my Tivo now playing list with python. ... username = 'tivo' ...
    (comp.lang.python)
  • Re: Authentication against system users and shadow passwords
    ... >> authentication against system users and their shadow passwords to ... >> pass a username and password and return have a boolean value returned. ... > Here's the guts of a Python function that does what you want. ... > # Validate A Username, Password, Group Tuple ...
    (comp.unix.bsd.freebsd.misc)
  • Setting Window Size using Pack Under Tkinter
    ... window (or any top level window) and to stop it being resized........ ... Yet Another Python Links Page ... Atlantibots - stomping across the worlds of Atlantis. ... What is rare is the courage to follow talent to the dark place where it leads. ...
    (comp.lang.python)