HTTP GET request with basic authorization?
Christopher J. Bottaro
cjbottaro at alumni.cs.utexas.edu
Mon Jan 3 21:42:27 EST 2005
John J. Lee wrote:
> 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
>
Thank you all. The webpage I was reading didn't say anything about base64
encoding the authentication string. Also thanks for the tip on the urllib2
handlers.
More information about the Python-list
mailing list