HTTP Digest/Basic Auth with Python Requests module HTTP Digest/Basic Auth with Python Requests module python-3.x python-3.x

HTTP Digest/Basic Auth with Python Requests module


The two requests are independent:

r = requests.get(url, auth=HTTPDigestAuth('user', 'pass')) response = requests.get(url) #XXX <-- DROP IT

The second request does not send any credentials. Therefore it is not surprising that it receives 401 Unauthorized http response status.

To fix it:

  1. Use the same url as you use in your browser. Drop digest-auth/auth/user/pass at the end. It is just an example in the requests docs
  2. Print r.status_code instead of response.status_code to see whether it's succeeded.

Why would you use username/password in the url and in auth parameter? Drop username/password from the url. To see the request that is sent and the response headers, you could enable logging/debugging:

import loggingimport requestsfrom requests.auth import HTTPDigestAuth# these two lines enable debugging at httplib level (requests->urllib3->httplib)# you will see the REQUEST, including HEADERS and DATA, # and RESPONSE with HEADERS but without DATA.# the only thing missing will be the response.body which is not logged.try:    import httplibexcept ImportError:    import http.client as httplibhttplib.HTTPConnection.debuglevel = 1logging.basicConfig(level=logging.DEBUG) # you need to initialize logging,                       # otherwise you will not see anything from requests# make requesturl = 'https://example.com/cgi/metadata.cgi?template=html'r = requests.get(url, auth=HTTPDigestAuth('myUsername', 'myPassword'),                 timeout=10)print(r.status_code)print(r.headers)


import requestsfrom requests.auth import HTTPDigestAuthurl='https://example.com/cgi/metadata.cgi?template=html'r = requests.get(url, auth=HTTPDigestAuth('myUsername', 'myPassword'), verify=False,  stream=True)        print(r.headers) print(r.status_code)

fixed with adding stream=True since the page is streaming xml/html data. My next questions is, how do I store/parse a constant stream of data?

I tried storing in r.content but it seems to run indefinitely (the same problem I had before)