Q: Authentication with httplib
C.Laurence Gonsalves
clgonsal at keeshah.penguinpowered.com
Mon Aug 23 16:08:47 EDT 1999
On Mon, 23 Aug 1999 11:27:22 +0200, Zwika Moshkowzki <zwi at turboimage.com> wrote:
> Hi,
>
> I am using httplib to access a Page on a Server:
>
> .....
> h = httplib.HTTP(Server)
> h.putrequest('GET', Page)
> h.endheaders()
> .....
>
> When Server is configured not to require a passwd, I set Server="myServer",
> Page="/myPage",
> and everything is OK.
>
> My Q regards the situation where there is a need for authentication: Where
> should I put the Username+Passwd?
>
> Thanks in advance!
The username and password need to go in an "Authorization" header.
Something like this should work:
import base64
import string
import httplib
userid = "foo"
passwd = "bar"
# "Basic" authentication encodes userid:password in base64. Note
# that base64.encodestring adds some extra newlines/carriage-returns
# to the end of the result. string.strip is a simple way to remove
# these characters.
auth = 'Basic ' + string.strip(base64.encodestring(userid + ':' + passwd))
h = httplib.HTTP(Server)
h.putrequest('GET', Page)
h.putheader('Authorization', auth )
h.endheaders()
Hope that helps...
--
C. Laurence Gonsalves "Any sufficiently advanced
clgonsal at kami.com technology is indistinguishable
http://www.cryogen.com/clgonsal/ from magic." -- Arthur C. Clarke
More information about the Python-list
mailing list