Skip to content
Myr

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

1 file+0 added−142 removed

deleted:requests/monkeys.py-142 removed
1#-*- coding: utf-8 -*-23"""4requests.monkeys5~~~~~~~~~~~~~~~~67Urllib2 Monkey patches.89"""1011import re12import urllib2131415class Request(urllib2.Request):16 """Hidden wrapper around the urllib2.Request object. Allows for manual17 setting of HTTP methods.18 """1920 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 = method2324 def get_method(self):25 if self.method:26 return self.method2728 return urllib2.Request.get_method(self)293031class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):32 """HTTP Redirect handler."""33 def http_error_301(self, req, fp, code, msg, headers):34 pass3536 http_error_302 = http_error_303 = http_error_307 = http_error_301373839class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):40 """HTTP Basic Auth Handler with authentication loop fixes."""4142 def __init__(self, *args, **kwargs):43 urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)44 self.retried_req = None45 self.retried = 04647 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 pass5253 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 = 05859 return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(60 self, auth_header, host, req, headers61 )626364class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):65 """HTTP Basic Auth Handler with forced Authentication."""6667 auth_header = 'Authorization'68 rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'69 'realm=(["\'])(.*?)\\2', re.I)7071 def __init__(self, *args, **kwargs):72 HTTPBasicAuthHandler.__init__(self, *args, **kwargs)7374 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 response7980 http_error_404 = http_error_4018182 def _http_error_auth_reqed(self, authreq, host, req, headers):8384 authreq = headers.get(authreq, None)8586 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 += 19293 if authreq:9495 mo = self.rx.search(authreq)9697 if mo:98 scheme, quote, realm = mo.groups()99100 if scheme.lower() == 'basic':101 response = self.retry_http_basic_auth(host, req, realm)102103 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')108109 if response and response.code not in (401, 404):110 self.retried = 0111 return response112113114class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):115116 def __init__(self, *args, **kwargs):117 urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)118 self.retried_req = None119120 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 pass125126 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.134135 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