我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用requests.exceptions.TooManyRedirects()。
def single_thread_solve(): """ ???????????????, ????????, ?????????? """ with open("solve_result.txt", "w") as f: check_codes = dict() for i in range(355, 1000 + 1): # ??? 1 ? 1000 ?????????? check_code = get_verify_code(i, check_codes, f) # ???? url = ("http://www.qlcoder.com/train/handsomerank?_token=d4texP05ci7veIAztvnwe5yETOFhlLWkSaBYC51B" "&user=w%40tch&checkcode={}".format(check_code)) while True: try: response = requests.get(url, timeout=10) if "?????" not in response.text: print("[+] ????? {} ?".format(i), file=f) except (ConnectTimeout, ReadTimeout, ValueError, ConnectionError, TooManyRedirects): print("[-] ??? {} ???".format(i), file=f) else: break
def signature_verified(self): try: url = 'https://%s/.well-known/poet.jwk' % (self.iss) r = requests.get(url, timeout=3) if r.status_code == 200: k = json.loads(r.text) payload = verify_poet(self.jwt, k) if 'iss' in payload: if payload['iss'] == k['kid']: return True except ConnectionError: pass except TooManyRedirects: pass except Timeout: pass return False
def check_connection(url): try: request = requests.head(url) if request.status_code == 200: print('Connection ok...') return True elif request.status_code == 301: print('Connection redirect') return True else: print('Error connecting') return False except (ConnectionError, Timeout): print('{} can not be reached, check your connection and retry later'.format(url)) return False except (HTTPError, TooManyRedirects): print('There is an issue with the url, {}, confirm it, or retry later'.format(url)) return False
def http(self, method, host, payload=''): print 'HTTP %s %s: %s' % (method, host, payload) methodfn = getattr(requests, method.lower(), None) if method is None: raise NotImplementedError("requests module has not method %s " % method) try: if payload != '': request = methodfn(url='%s/%s' % (self.hosturl, host), data=json.dumps(payload), auth=self.auth, verify=False) else: request = methodfn(url='%s/%s' % (self.hosturl, host), auth=self.auth, verify=False) if request.status_code != requests.codes.ok: request.raise_for_status() rc = 0 out = json.loads(request.text) err = '' except (ConnectionError, HTTPError, Timeout, TooManyRedirects) as e: rc = 1 out = '' err = '%s. Error received: %s.\n Sent request: %s' % ( e.message, json.loads(request.text), 'HTTP %s %s: %s' % (method, host, payload)) print 'HTTP %s returned: %s' % (method, request.text) return (rc, out, err)
def test_re_raised_exceptions(self): my_exceptions = (exceptions.ConnectionError, exceptions.URLRequired, exceptions.TooManyRedirects, exceptions.Timeout, exceptions.RequestException) for e in my_exceptions: self.response.raise_for_status.side_effect = e with assert_raises(e): self.client._do_request('GET','MyCommand')
def retry(retries=CONFIG.CRAWLER.RETRY or 3, sleep=CONFIG.CRAWLER.SLEEP, changeip=False): """??????????????????????retrying pip install retrying https://github.com/rholder/retrying ?????????? http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=2653547274&idx=1&sn=52e5037b163146c1656eedce2da1ecd8&scene=1&srcid=0527MEXhNRZATtlTPhinD5Re#rd :param retries: number int of retry times. 301 Moved Temporarily 401 Unauthorized 403 Forbidden 404 Not Found 408 Request Timeout 429 Too Many Requests 503 Service Unavailable """ def _retry(func): @wraps(func) def _wrapper(*args, **kwargs): index = 0 while index < retries: index += 1 try: response = func(*args, **kwargs) if response.status_code in (301, 302, 404, 500): print('status_code', response.status_code) break elif response.status_code != 200: print(response.status_code) if changeip: change_ip() continue else: break except Exception as e: # ??????????? # traceback.print_exc() response = None if isinstance(e, Timeout): if sleep is not None: time.sleep(sleep + randint(1, 10)) continue elif isinstance(e, TooManyRedirects): break return response return _wrapper return _retry