urllib, python 1.52, basic authentication
Steve Holden
sholden at holdenweb.com
Wed Apr 18 19:55:31 EDT 2001
The last time someone asked this question I suggested using a URL like
http://username:password@host/path/filename.ext
which seems to do the trick.
regards
Steve
"Ari Davidow" <ari at ivritype.com> wrote in message
news:mailman.987629221.4770.python-list at python.org...
> 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