Re: HTTP GET request with basic authorization?

Discussion in 'Python' started by John J. Lee, Jan 3, 2005.

  1. John J. Lee

    John J. Lee Guest

    Jonas Galvez <> writes:

    > Christopher J. wrote:
    > > I tried this, but it didn't work:
    > > conn.request("GET", "/somepage.html", None,
    > > {"AUTHORIZATION": "Basic username:password"})

    [...]
    > import re, base64, urllib2
    >
    > userpass = ('user', 'pass')
    > url = 'http://somewhere'
    >
    > request = urllib2.Request(url)
    > authstring = base64.encodestring('%s:%s' % userpass)
    > authstring = authstring.replace('\n', '')
    > request.add_header("Authorization", "Basic %s" % authstring)
    >
    > content = urllib2.urlopen(request).read()


    There are handlers in urllib2 to do this for you, you shouldn't need
    to do it by hand. I rarely do, so I won't risk an example...


    John
     
    John J. Lee, Jan 3, 2005
    #1
    1. Advertising

  2. John J. Lee

    John Reese Guest

    On 03 Jan 2005 18:27:52 +0000, John J. Lee <> wrote:
    > Jonas Galvez <> writes:
    >
    >> Christopher J. wrote:
    >> > I tried this, but it didn't work:
    >> > conn.request("GET", "/somepage.html", None,
    >> > {"AUTHORIZATION": "Basic username:password"})

    > [...]
    >> import re, base64, urllib2
    >>
    >> userpass = ('user', 'pass')
    >> url = 'http://somewhere'
    >>
    >> request = urllib2.Request(url)
    >> authstring = base64.encodestring('%s:%s' % userpass)
    >> authstring = authstring.replace('\n', '')
    >> request.add_header("Authorization", "Basic %s" % authstring)
    >>
    >> content = urllib2.urlopen(request).read()

    >
    > There are handlers in urllib2 to do this for you, you shouldn't need
    > to do it by hand. I rarely do, so I won't risk an example...
    >
    >
    > John
    >


    The way to do it with the handlers follows; oddly, it's just about as
    long.

    import urllib2

    url = 'http://somewhere'
    realm, user, pass = 'realm', 'user', 'pass'
    hm = urllib2.HTTPPasswordMgrWithDefaultRealm()
    hm.add_password(realm, url, user, pass)
    od = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(hm))
    content = od.open(url).read()
     
    John Reese, Jan 4, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher J. Bottaro

    HTTP GET request with basic authorization?

    Christopher J. Bottaro, Jan 2, 2005, in forum: Python
    Replies:
    1
    Views:
    612
    Fuzzyman
    Jan 4, 2005
  2. Jonas Galvez
    Replies:
    0
    Views:
    610
    Jonas Galvez
    Jan 2, 2005
  3. SeanRW
    Replies:
    1
    Views:
    417
    Dominick Baier [DevelopMentor]
    May 25, 2006
  4. Nacho Nachev
    Replies:
    2
    Views:
    945
  5. scottny07
    Replies:
    1
    Views:
    231
    Peter Scott
    Mar 30, 2007
Loading...

Share This Page