monkeys are deprecated
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 37ed911 · Browse files · 1 file·+0 added−142 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+0 added−142 removed
- other:+0 added−142 removed
Provenance
- Commit
33df9d1- Tree
e9dec41- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
37ed911
1 file changed+0 added−142 removed
deleted:requests/monkeys.py-142 removed
1-#-*- coding: utf-8 -*-2-3-"""4-requests.monkeys5-~~~~~~~~~~~~~~~~6-7-Urllib2 Monkey patches.8-9-"""10-11-import re12-import urllib213-14-15-class Request(urllib2.Request):16- """Hidden wrapper around the urllib2.Request object. Allows for manual17- setting of HTTP methods.18- """19-20- def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None):21- urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)22- self.method = method23-24- def get_method(self):25- if self.method:26- return self.method27-28- return urllib2.Request.get_method(self)29-30-31-class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):32- """HTTP Redirect handler."""33- def http_error_301(self, req, fp, code, msg, headers):34- pass35-36- http_error_302 = http_error_303 = http_error_307 = http_error_30137-38-39-class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):40- """HTTP Basic Auth Handler with authentication loop fixes."""41-42- def __init__(self, *args, **kwargs):43- urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)44- self.retried_req = None45- self.retried = 046-47- def reset_retry_count(self):48- # Python 2.6.5 will call this on 401 or 407 errors and thus loop49- # forever. We disable reset_retry_count completely and reset in50- # http_error_auth_reqed instead.51- pass52-53- def http_error_auth_reqed(self, auth_header, host, req, headers):54- # Reset the retry counter once for each request.55- if req is not self.retried_req:56- self.retried_req = req57- self.retried = 058-59- return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(60- self, auth_header, host, req, headers61- )62-63-64-class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):65- """HTTP Basic Auth Handler with forced Authentication."""66-67- auth_header = 'Authorization'68- rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'69- 'realm=(["\'])(.*?)\\2', re.I)70-71- def __init__(self, *args, **kwargs):72- HTTPBasicAuthHandler.__init__(self, *args, **kwargs)73-74- def http_error_401(self, req, fp, code, msg, headers):75- url = req.get_full_url()76- response = self._http_error_auth_reqed('www-authenticate', url, req, headers)77- self.reset_retry_count()78- return response79-80- http_error_404 = http_error_40181-82- def _http_error_auth_reqed(self, authreq, host, req, headers):83-84- authreq = headers.get(authreq, None)85-86- if self.retried > 5:87- # retry sending the username:password 5 times before failing.88- raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",89- headers, None)90- else:91- self.retried += 192-93- if authreq:94-95- mo = self.rx.search(authreq)96-97- if mo:98- scheme, quote, realm = mo.groups()99-100- if scheme.lower() == 'basic':101- response = self.retry_http_basic_auth(host, req, realm)102-103- if response and response.code not in (401, 404):104- self.retried = 0105- return response106- else:107- response = self.retry_http_basic_auth(host, req, 'Realm')108-109- if response and response.code not in (401, 404):110- self.retried = 0111- return response112-113-114-class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):115-116- def __init__(self, *args, **kwargs):117- urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)118- self.retried_req = None119-120- def reset_retry_count(self):121- # Python 2.6.5 will call this on 401 or 407 errors and thus loop122- # forever. We disable reset_retry_count completely and reset in123- # http_error_auth_reqed instead.124- pass125-126- def http_error_auth_reqed(self, auth_header, host, req, headers):127- # Reset the retry counter once for each request.128- if req is not self.retried_req:129- self.retried_req = req130- self.retried = 0131- # In python < 2.5 AbstractDigestAuthHandler raises a ValueError if132- # it doesn't know about the auth type requested. This can happen if133- # somebody is using BasicAuth and types a bad password.134-135- try:136- return urllib2.HTTPDigestAuthHandler.http_error_auth_reqed(137- self, auth_header, host, req, headers)138- except ValueError, inst:139- arg = inst.args[0]140- if arg.startswith("AbstractDigestAuthHandler doesn't know "):141- return142- raise