urllib, python 1.52, basic authentication

Ari Davidow ari at ivritype.com
Wed Apr 18 17:25:22 EDT 2001


Hi,

I am trying to request a page that is being protected by basic authentication.

So, I run something like:

stuff = "data=urllib.urlencode({'username': 'foo', 'password' : 'foopw'})"
urllib.urlopen('http://www.site.com/admin/flushgo.html?ok=ok',stuff)

or

urllib.urlopen('http://username:password@www.site.com/admin/flushgo.html')

and in either case, I get requested for the name and password.

I've tried various incarnations of using httplib, but clearly I don't understand what is needed. Most promising seems:

import httplib, urllib,binascii
cookie = binascii.b2a_base64("user:password")
h = httplib.HTTP("www.site.com")
h.putrequest("GET", "/admin/flushgo.html?ok=ok" "HTTP/1.0\r\n")
h.putheader("Proxy-authorization", "Basic %s" % cookie)
h.endheaders()
reply, msg, hdrs = h.getreply()
print reply # should be 200
data = h.getfile().read() # get the raw HTML

which gets me an error on the last line about "none" not being a legit result.
If I omit the last line, I get a "-1" response, which also seems broken.

How does one approach this with python 1.52 (please please please don't suggest that I make a production system dependent on urllib2 with just one more patch in the beta of python 2.1 or such--that is a place I'd like to avoid)?

ari


Ari Davidow
ari at ivritype.com
http://www.ivritype.com/




More information about the Python-list mailing list