HTTP GET request with basic authorization?

Jonas Galvez jonasgalvez at
Sun Jan 2 18:12:39 EST 2005

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

Hmm, try this:

import re, base64
userpass = base64.encodestring('user:pass').replace('\n', '')
authd = {'Authorization':'Basic %s' % userpass}
conn.request('GET', '/uri', None, authd)

Or this:

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()

More information about the Python-list mailing list