HTTP GET request with basic authorization?
John J. Lee
jjl at pobox.com
Mon Jan 3 13:27:52 EST 2005
Jonas Galvez <jonasgalvez at gmail.com> 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
More information about the Python-list
mailing list