Skip to content
Snippets Groups Projects
Commit 14ff387e authored by Guido Trotter's avatar Guido Trotter
Browse files

RapiClient: fix multi-authentication in Python 2.6


In Python 2.6 the urllib2.HTTPBasicAuthHandler has a "retried" count for
failed authentications. The handler fails after 5 of them. To solve this
we reset the handler's "retried" member variable to 0 after every
successful request. This is a bit ugly, but makes the client work again
for more than 5 requests under all versions of Python.

Note that the digest authentication handler has a reset_retry_count()
method to do this, but the method is not defined for the basic
authentication handler, so we must reset the variable itself.

This member variable is unused in 2.4 and 2.5, so the change doesn't
affect the client under older Python versions.

Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 87e058d5
No related branches found
No related tags found
No related merge requests found
......@@ -357,10 +357,12 @@ class GanetiRapiClient(object):
handlers = [_HTTPSHandler(self._logger, config_ssl_verification)]
self._httpauthhandler = None
if username is not None:
pwmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwmgr.add_password(None, self._base_url, username, password)
handlers.append(urllib2.HTTPBasicAuthHandler(pwmgr))
self._httpauthhandler = urllib2.HTTPBasicAuthHandler(pwmgr)
handlers.append(self._httpauthhandler)
elif password:
raise Error("Specified password without username")
......@@ -450,6 +452,11 @@ class GanetiRapiClient(object):
try:
resp = self._http.open(req)
encoded_response_content = resp.read()
# Workaround for python 2.6: reset the retried count of the http handler,
# or it'll fail after ~5 requests made with the same client. Under python
# 2.4 and 2.5 this variable is not used.
if self._httpauthhandler is not None:
self._httpauthhandler.retried = 0
except (OpenSSL.SSL.Error, OpenSSL.crypto.Error), err:
raise CertificateError("SSL issue: %s (%r)" % (err, err))
except urllib2.HTTPError, err:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment