我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用requests.exceptions.Timeout()。
def update_telemetry(navsat_msg, compass_msg): """Telemetry subscription callback. Args: navsat_msg: sensor_msgs/NavSatFix message. compass_msg: std_msgs/Float64 message in degrees. """ try: client.post_telemetry(navsat_msg, compass_msg) except (ConnectionError, Timeout) as e: rospy.logwarn(e) return except (ValueError, HTTPError) as e: rospy.logerr(e) return except Exception as e: rospy.logfatal(e) return
def get_cids(self, cas): """ Use the PubChem API to get the CID :param cas: string - CAS identifier :return: list of CIDs """ uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/%s/cids/json" \ "?email=%s" try: response = get((uri % (cas, app.config['ADMIN_EMAIL']))).json() try: cids = response['IdentifierList']['CID'] return cids except KeyError: return None except (exceptions.ConnectionError, TimeoutError, exceptions.Timeout, exceptions.ConnectTimeout, exceptions.ReadTimeout) as e: # Error. return the error and the CAS number that this error occured on sys.stderr.write("Error: %s. Occurred on CAS: %s", (e, cas)) sys.stderr.flush() sys.stdout.flush()
def hard_requests(): from requests import Request, Session s = Session() headers = {'User-Agent': 'fake1.3.4'} req = Request('GET', build_uri('user/emails'), auth=('shfanzie', '**********'), headers = headers) prepped = req.prepare() print prepped.body print prepped.headers try: resp = s.send(prepped, timeout=1) resp.raise_for_status() except exceptions.Timeout as e: print e.message except exceptions.HTTPError as e: print e.message else: print resp.status_code print resp.reason print resp.request.headers print resp.text
def test_retry_timeout_404_return_none(self, sleep, request): """Test HTTP request via call() with timeouts (finishing with 404 but return None).""" request.side_effect = [ Timeout, ConnectionError, timeout, fake_response({}, 404), Timeout, ConnectionError, timeout ] resp = call('http://genie-timeout-404', attempts=7, backoff=0, failure_codes=404, none_on_404=True) assert_equals(4, request.call_count) assert_equals(3, sleep.call_count) assert_equals(None, resp)
def get_tags(self, lang=None, auth=None): """ Gets all the tags inside the wordpress application """ params = {} query = self.wp_url + "wp-json/taxonomies/post_tag/terms/" if lang is not None: params['lang'] = lang try: response = requests.get( query, params=params, timeout=30, auth=auth) except (ConnectionError, Timeout): return {'server_error': 'The server is not reachable this moment\ please try again later'} if response.status_code != 200: return { 'server_error': 'Server returned status code %i' % response. status_code} return response.json()
def get_categories(self, lang=None, auth=None): """ Gets all the categories inside the wordpress application """ params = {} query = self.wp_url + "wp-json/taxonomies/category/terms/" if lang is not None: params['lang'] = lang try: response = requests.get( query, params=params, timeout=30, auth=auth) except (ConnectionError, Timeout): return {'server_error': 'The server is not reachable this moment\ please try again later'} if response.status_code != 200: return { 'server_error': 'Server returned status code %i' % response. status_code} return response.json()
def set_image(instance, field, width=1920, height=1440): """ FileField / ImageField """ manager = getattr(instance, field.name) try: image_type = random.choice(['people', 'places', 'things']) response = requests.get( 'https://placem.at/%s?w=%d&h=%d&random=1&txt=' % (image_type, width, height), timeout=5, stream=True ) except (ConnectionError, Timeout): response = requests.get('http://baconmockup.com/%d/%d/' % (width, height), stream=True) tfp = tempfile.NamedTemporaryFile(delete=False) with tfp: for chunk in response.iter_content(1024 * 1024): tfp.write(chunk) tfp.seek(0) manager.save('image.jpg', File(tfp), save=False)
def handle(self, *args, **options): interval = float(options.get("interval", DEFAULT_PING_INTERVAL)) checkrate = float(options.get("checkrate", DEFAULT_PING_CHECKRATE)) server = options.get("server", DEFAULT_PING_SERVER_URL) self.started = datetime.now() while True: try: logging.info("Attempting a ping.") data = self.perform_ping(server) logging.info("Ping succeeded! (response: {}) Sleeping for {} minutes.".format(data, interval)) time.sleep(interval * 60) continue except ConnectionError: logging.warn("Ping failed (could not connect). Trying again in {} minutes.".format(checkrate)) except Timeout: logging.warn("Ping failed (connection timed out). Trying again in {} minutes.".format(checkrate)) except RequestException as e: logging.warn("Ping failed ({})! Trying again in {} minutes.".format(e, checkrate)) time.sleep(checkrate * 60)
def _fetch_data(urls): """Function obtains ip address by query Args: urls : list of urls used for query Return: ip address of machine """ logging.getLogger("requests").setLevel(logging.WARNING) for url in urls: try: req = requests.get(url, timeout=3) if req.status_code == 200: data = req.text.strip() if data is None or not _verify_address(data): continue else: return data else: raise ConnectionError except (Timeout, ConnectionError): logger.warning('Could not fetch public ip from %s', url) return None
def test_v2_authentication_with_intermittent_failures(self, get_mock, post_mock): # Requests timeout exception timeout_ex = req_ex.Timeout('Timeout') get_mock.return_value = self._valid_get_response() post_mock.side_effect = [self._valid_post_response(), timeout_ex, timeout_ex, self._valid_post_response()] identity = v2.RackspaceIdentity.from_username( self.username, self.password, user_domain_id=self.domain_id) self.assertRaises(exception.BadGateway, identity.authenticate) self.assertRaises(exception.BadGateway, identity.authenticate) self.assertIsNotNone(identity.authenticate())
def fetch_prs(self): """Gets the PRs for the configured repository. Returns: A tuple of (pr_list, error). pr_list is an iterable of GithubPRs if successful, otherwise empty. error is None if successful, or the error message otherwise. """ try: prs = self.get(GITHUB_PULLS_ENDPOINT) return [GithubPR(self, pr) for pr in prs], None except HTTPError as exc: return [], 'Non-200 HTTP Code Received: {}'.format(exc) except Timeout as exc: return [], 'Request timed out: {}'.format(exc) except RequestException as exc: return [], 'Catastrophic error in requests: {}'.format(exc)
def get(self, endpoint): """get makes a GET request against a specified endpoint. Args: endpoint: URL to which to make the GET request. URL is relative to https://api.github.com/repos/{self.org}/{self.repo}/ Returns: JSON object retrieved from the endpoint. Raises: HTTPError: if we get an HTTP error status. Timeout: for timeouts. RequestException: for other assorted exceptional cases. """ url = urlparse.urljoin(self.repo_url, endpoint) resp = requests.get(url, auth=(BOT_NAME, self.bot_key)) if resp.status_code is 200: return resp.json() resp.raise_for_status()
def check_domains(self): errors = [] try: errors = [domain.create_slack_message() for domain in self.domains if not domain.check_domain()] except (NXDOMAIN.DNSException, SyntaxError, Timeout, FormError, TooBig) as e: self.slack.post_message(unicode(e.message)) self.logger.error(unicode(e.message)) for error in errors: self.slack.post_message(error) self.logger.error(error) return len(errors)
def fetch_public_key(app): """Get public key and caches it on the app object for future use.""" # TODO: even though saving the key on the app object is not very nice, # it's actually safe - the worst thing that can happen is that we will # fetch and save the same value on the app object multiple times if not getattr(app, 'public_key', ''): keycloak_url = app.config.get('BAYESIAN_FETCH_PUBLIC_KEY', '') if keycloak_url: pub_key_url = keycloak_url.strip('/') + '/auth/realms/fabric8/' try: result = get(pub_key_url, timeout=0.5) app.logger.info('Fetching public key from %s, status %d, result: %s', pub_key_url, result.status_code, result.text) except exceptions.Timeout: app.logger.error('Timeout fetching public key from %s', pub_key_url) return '' if result.status_code != 200: return '' pkey = result.json().get('public_key', '') app.public_key = \ '-----BEGIN PUBLIC KEY-----\n{pkey}\n-----END PUBLIC KEY-----'.format(pkey=pkey) else: app.public_key = app.config.get('BAYESIAN_PUBLIC_KEY') return app.public_key
def publish_obstacles(timer_event): """Requests and publishes obstacles. Args: timer_event: ROS TimerEvent. """ try: moving_obstacles, stationary_obstacles = client.get_obstacles( frame, lifetime) except (ConnectionError, Timeout) as e: rospy.logwarn(e) return except (ValueError, HTTPError) as e: rospy.logerr(e) return except Exception as e: rospy.logfatal(e) return moving_pub.publish(moving_obstacles) stationary_pub.publish(stationary_obstacles)
def get_active_mission(req): """ Service to update mission information with current active mission. Args: req: Request of type Trigger. Returns: TriggerResponse with true, false for success, failure. """ with lock: global msgs try: msgs = client.get_active_mission(frame) except (ConnectionError, Timeout) as e: rospy.logwarn(e) return False, str(e) except (ValueError, HTTPError) as e: rospy.logerr(e) return False, str(e) rospy.loginfo("Using active mission") return True, "Success"
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 getotherinfo(train_number = 'G24'): ''' ??: ????: G24? ??: '2016-04-30'; ??????????? GXX/GXX ???????‘????????’ ''' logging.info('getotherinfo') url = 'http://train.qunar.com/qunar/checiSuggest.jsp?callback=jQuery17208000492092391186_1460000280989&include_coach_suggest=true&lang=zh&q='+train_number+'&sa=true&format=js&_=1460000429009' try: response = requests.get(url=url, timeout=10) except Timeout: logging.error('??????????') logging.error('url: '+url) return None results=json.loads('{'+response.text.split('({')[1].split('})')[0]+'}')['result'] for result in results: opendatafile = open('./data/????????','r') lines=opendatafile.readlines() if ('/' in result['key']) & (result['key']+'\n' not in lines): gotfile = open('./data/????????','a') gotfile.write(result['key']) gotfile.write('\n') gotfile.close() opendatafile.close()
def _send_message_to_hermes(url, headers, json_data): """ Send message to hermes with retrying. """ with requests.Session() as session: _handle_request_adapter(session) try: resp = session.post(url, headers=headers, data=json_data) except (ConnectionError, HTTPError, Timeout) as e: message = 'Error pushing event to Hermes: {}.'.format(e) raise HermesPublishException(message) if resp.status_code not in HERMES_VALID_RESPONSE_CODES: message = 'Bad response code during Hermes push: {}.'.format( resp.status_code ) raise HermesPublishException(message) return resp
def test_server_time(self): """Test the server_time method""" # On a normal query, the time should be close to the # current time. There is a lot of jitter on the time # returned by the server, so we judge that a # difference of 1 minute is ok. kraken = Kraken() server_time = kraken.server_time() current_time = time.time() self.assertTrue(abs(server_time - current_time) < 60) time.sleep(3) # Set the timeout limit to a very low value to be # sure it is raised. kraken.timeout = 1e-16 self.assertRaises(Timeout, kraken.server_time) kraken.timeout = 5.0 time.sleep(3) # Change the url to get and HTTP error. kraken.url = kraken.url[:-1] self.assertRaises(HTTPError, kraken.server_time)
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 exception_handle(method): """Handle exception raised by requests library.""" def wrapper(*args, **kwargs): try: result = method(*args, **kwargs) return result except ProxyError: LOG.exception('ProxyError when try to get %s.', args) raise ProxyError('A proxy error occurred.') except ConnectionException: LOG.exception('ConnectionError when try to get %s.', args) raise ConnectionException('DNS failure, refused connection, etc.') except Timeout: LOG.exception('Timeout when try to get %s', args) raise Timeout('The request timed out.') except RequestException: LOG.exception('RequestException when try to get %s.', args) raise RequestException('Please check out your network.') return wrapper
def streaming(credentials, coins, queries, refresh, path, realtime=False, logTracker=True, logTweets=True, logSentiment=False, debug=True): # User Error Checks if len(coins) <= 0: print("Error: You must include at least one coin."); return if len(coins) >= 10: print("Warning: Fewer than ten coins recommended.") if len(queries) <= 0: print("Error: You must include at least one query."); return if len(queries) >= 20: print("Warning: Fewer than twenty queries recommended.") if refresh <= 0: print("Error: Refresh rate must be greater than 0"); return auth = tweepy.OAuthHandler(credentials[0], credentials[1]) auth.set_access_token(credentials[2], credentials[3]) if logSentiment: global SentimentIntensityAnalyzer from nltk.sentiment.vader import SentimentIntensityAnalyzer while True: # Start streaming ----------------------------- try: print("Streaming Now...") listener = CoinListener(auth, coins, queries, refresh, path, realtime, logTracker, logTweets, logSentiment, debug) stream = tweepy.Stream(auth, listener) stream.filter(track=queries) except (Timeout, ConnectionError, ReadTimeoutError): print("Reestablishing Connection...") with open("%sError_Log.txt" % path, "a") as outfile: outfile.write("%s Error: Connection Dropped\n" % time.strftime('%m/%d/%Y %H:%M')) time.sleep((15*60)+1) # Wait at least 15 minutes before restarting listener # ---------------------------------------------
def requests_get(url, **kwargs): USER_AGENTS = ( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100 101 Firefox/22.0', 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0', ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) ' 'Chrome/19.0.1084.46 Safari/536.5'), ('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46' 'Safari/536.5') ) try: r = requests.get( url, timeout=12, headers={'User-Agent': random.choice(USER_AGENTS)}, **kwargs ) except ConnectionError: exit_after_echo('Network connection failed.') except Timeout: exit_after_echo('timeout.') return r
def _check_connector_backend_access(url, timeout=120): max_time = datetime.now() + timedelta(seconds=timeout) while True: try: response = get(url=url, timeout=10) if response.status_code == 200: print() return raise_by_max_time("Waiting time exceeded", max_time) except SSLError: raise_by_max_time("An SSL error occurred", max_time) except Timeout: raise_by_max_time("Timeout connecting to Connector Backend", max_time) except ConnectionError: raise_by_max_time("Connection error to Connector Backend", max_time) except Exception as e: raise_by_max_time(str(e), max_time) time.sleep(10)
def simple_retries_decorator(func): @functools.wraps(func) def wrapped_call(*args, **kwargs): retry_count = 0 while (retry_count < 3): try: return func(*args, **kwargs) except (Timeout, ConnectionError) as networking_error: retry_count += 1 post_process_retry(retry_count, 3, networking_error) except oci.exceptions.ServiceError as service_error: if is_retryable_service_error(service_error): retry_count += 1 post_process_retry(retry_count, 3, service_error) else: raise return wrapped_call
def _is_exception_retryable(e): """ Determines if the service should attempt to retry an operation based on the type of exception retry if timeout Connection error unknown client exception: status == -1 server exception: status >= 500 Potential edge case: status == 409 :param e: Exception :return: Boolean """ retryable = False if isinstance(e, Timeout): retryable = True elif isinstance(e, ConnectionError): retryable = True elif isinstance(e, ServiceError): if e.status >= 500 or e.status == -1 or (e.status == 409 and e.code == "ConcurrentObjectUpdate"): retryable = True return retryable
def login(self): try: self.session = requests.session() self.session.stream = False login_data = dict(username=self.server_user, password=self.server_password, client_id=self.client_id, client_secret=self.client_secret, grant_type="password") response = self.session.post(self.login_url, data=login_data, timeout=self.timeout, verify=False) if (response.status_code != 200): logger.info("login result %s %s", response.status_code, response.text) else: self.set_token(response.json()['access_token']) except Timeout as e: logger.warning('Login for %s timed out: %s' , self.login_url, e) raise e except Exception as e: logger.error(e) raise e return response
def request(self, method, url, params = {}, data = {}, files = None): post_values = data headers = {"Authorization": "Bearer %s" % self.access_token} try: response = self.session.request(method, url, params=params, data=post_values, files=files, headers=headers, timeout=self.timeout, verify=False) except Timeout as e: logger.warning('POST for %s timed out: %s', url, e) raise e if response.status_code == requests.codes['forbidden']: raise LoginException("forbidden status code received on %s %s %s", method, response.status_code, response.text) elif (response.status_code != requests.codes['ok']): self.access_token = None logger.error("raised exception, resetting token %s", response.status_code) # read content to let it know we are done with response.content return response
def login(self): try: self.session = requests.session() self.session.stream = False self.session.get(self.login_url, verify=False, timeout=self.timeout) self.csrf_token = self.session.cookies['csrftoken'] login_data = dict(username=self.server_user, password=self.server_password, csrfmiddlewaretoken=self.csrf_token) headers = {"X-CSRFToken": self.csrf_token, "Referer": "%s://%s" % (self.server_protocol, self.server_host)} response = self.session.post(self.login_url, data=login_data, headers=headers, timeout=self.timeout, verify=False) if (response.status_code != 200): logger.info("login result %s %s" % (response.status_code, response.text)) except Timeout as e: logger.warning('Login for %s timed out: %s' % (self.login_url, e)) raise e except Exception as e: logger.error(e) raise e return response
def check(auth_ref, args): # We call get_keystone_client here as there is some logic within to get a # new token if previous one is bad. keystone = get_keystone_client(auth_ref) auth_token = keystone.auth_token registry_endpoint = 'http://{ip}:9191'.format(ip=args.ip) s = requests.Session() s.headers.update( {'Content-type': 'application/json', 'x-auth-token': auth_token}) try: # /images returns a list of public, non-deleted images r = s.get('%s/images' % registry_endpoint, verify=False, timeout=10) is_up = r.ok except (exc.ConnectionError, exc.HTTPError, exc.Timeout): is_up = False except Exception as e: status_err(str(e)) metric_values = dict() status_ok() metric_bool('glance_registry_local_status', is_up) # only want to send other metrics if api is up if is_up: milliseconds = r.elapsed.total_seconds() * 1000 metric('glance_registry_local_response_time', 'double', '%.3f' % milliseconds, 'ms') metric_values['glance_registry_local_response_time'] = ('%.3f' % milliseconds) metric_influx(INFLUX_MEASUREMENT_NAME, metric_values)
def check(args): metadata_endpoint = ('http://{ip}:8775'.format(ip=args.ip)) is_up = True s = requests.Session() try: # looks like we can only get / (ec2 versions) without specifying # an instance ID and other headers versions = s.get('%s/' % metadata_endpoint, verify=False, timeout=10) milliseconds = versions.elapsed.total_seconds() * 1000 if not versions.ok or '1.0' not in versions.content.splitlines(): is_up = False except (exc.ConnectionError, exc.HTTPError, exc.Timeout) as e: is_up = False except Exception as e: status_err(str(e)) metric_values = dict() status_ok() metric_bool('nova_api_metadata_local_status', is_up) # only want to send other metrics if api is up if is_up: metric('nova_api_metadata_local_response_time', 'double', '%.3f' % milliseconds, 'ms') metric_values['nova_api_metadata_local_response_time'] = ('%.3f' % milliseconds) metric_influx(INFLUX_MEASUREMENT_NAME, metric_values)
def pubchem_counter(self, cid, collection): """ Use the SDQAgent that PubChem uses on their compound pages to get counts for a collection. cid: integer. The pubchem compound identifier Collection. String. One of the pubchem collections. E.g. "bioactivity" or "biocollection" Returns: Integer count """ uri = 'https://pubchem.ncbi.nlm.nih.gov/sdq/sdqagent.cgi?' \ 'infmt=json&outfmt=json' \ '&query={"select":["*"],"collection":"%s",' \ '"where":{"ors":{"cid":"%s"}},"start":1,"limit":1}' % (collection, cid) try: response = get(uri).json() try: count = response['SDQOutputSet'][0]['totalCount'] sys.stdout.write(str(count) + "\n") sys.stdout.flush() return count except KeyError: return None except (exceptions.ConnectionError, TimeoutError, exceptions.Timeout, exceptions.ConnectTimeout, exceptions.ReadTimeout) as e: # Error. return the error and the CID number that this error occured on # Save what have so far sys.stderr.write("Error: %s. Occurred on CID: %s", (e, cid)) sys.stderr.flush() sys.stdout.flush() quit() except exceptions.ChunkedEncodingError as e: sys.stderr.write("Error: %s. Occurred on CID: %s", (e, cid)) sys.stderr.flush() quit()
def test_get_handle_timeout(self): ''' Verify get with bad URL returns an error ''' dut = self.duts[0] self.clnt.connect([dut['node']], dut['username'], dut['password']) with self.assertRaises(Timeout): self.clnt.get('/tasks/getTasks.do', timeout=0.0001)
def _execute_task(self, task_id): ''' Execute a task and wait for it to complete. ''' # Test add_note_to_task self.api.add_note_to_task(task_id, 'Test Generated') self.api.execute_task(task_id) # Verify task executed within 30 seconds cnt = 30 while cnt > 0: time.sleep(1) result = self.api.get_task_by_id(task_id) status = result['workOrderUserDefinedStatus'] if status == 'Completed' or status == 'Failed': break cnt -= 1 err_msg = 'Execution for task id %s failed' % task_id self.assertNotEqual(status, 'Failed', msg=err_msg) err_msg = 'Timeout waiting for task id %s to execute' % task_id self.assertGreater(cnt, 0, msg=err_msg)
def test_api_request_timeout(self): ''' Verify api timeout ''' self.assertEqual(self.api.request_timeout, 30) self.api.request_timeout = 0.0001 with self.assertRaises(Timeout): self.api.get_cvp_info() self.api.request_timeout = 30.0
def test_get_timeout(self, mock): url = '{}/v2/i_dont_respond'.format(self.client.base_url) mock.get(url=url, exc=Timeout) with self.assertRaisesRegexp( newrelic_cli.exceptions.Timeout, 'Request timed out after \d+ seconds' ): self.client._get(url)
def run(self): while True: message = self.queue_in.get() # get() is blocking if not re.search(r'^(?:/|!)[ztib]?mensa', message.get_text().lower()): continue to_match = re.search(r'timeout=(\d*\.?\d+)', message.get_text().lower()) timeout = float(to_match.group(1)) if to_match else 2 chat_id = message.get_chat_id() data, filtr = compute_query(message.get_text().lower()) reply = "" try: for meal in filter(filtr, meal_list(data, timeout)): reply += "*" + meal.title + "*\n" reply += meal.description + "\n\n" if reply == "": reply = "Sadly you will have to starve :-(" except Timeout: reply = "Mensa timed out." self.bot.sendMessage(chat_id, reply, parse_mode="Markdown")
def time_request(): try: response = requests.get(build_uri('user/emails'), auth=('shfanzie', '**********'), timeout=10) response.raise_for_status() except exceptions.Timeout as e: print e.message except exceptions.HTTPError as e: print e.message else: print response.text print response.status_code # ???Request
def main(): url = sys.argv[1] mdk = start() session = mdk.session() session.setDeadline(1.0) mdk.stop() req_ssn = requests_session(session) try: req_ssn.get(url).content except Timeout: sys.exit(123) return
def test_retry_timeout(self, sleep, request): """Test HTTP request via call() with timeouts.""" request.side_effect = [ Timeout, ConnectionError, timeout, Timeout, ConnectionError, ] with assert_raises(Timeout): call('http://genie-timeout', attempts=4, backoff=0) assert_equals(4, request.call_count) assert_equals(3, sleep.call_count)
def test_retry_timeout_202(self, sleep, request): """Test HTTP request via call() with timeouts (finishing with 202).""" request.side_effect = [ Timeout, ConnectionError, fake_response({}, 202), timeout, Timeout ] call('http://genie-timeout-202', attempts=5, backoff=0) assert_equals(3, request.call_count) assert_equals(2, sleep.call_count)
def test_retry_timeout_404(self, sleep, request): """Test HTTP request via call() with timeouts with failure code.""" request.side_effect = [ Timeout, ConnectionError, timeout, fake_response({}, 404), Timeout, ConnectionError, timeout, Timeout, ConnectionError, timeout ] with assert_raises(GenieHTTPError): call('http://genie-timeout-404', failure_codes=404, attempts=7, backoff=0) assert_equals(4, request.call_count) assert_equals(3, sleep.call_count)
def download_image_validate(image_url): # Now download image and check type try: image = download_image(image_url) except HTTPError as e: # Non-2xx status code errors = ['URL returned a {} status code'.format(e.response.status_code)] raise ValidationError(errors) except Timeout as e: errors = ['URL timed out'] raise ValidationError(errors) except RequestException as e: # TODO log other errors print 'misc error' print image_url, e errors = ['URL is not a valid image'] raise ValidationError(errors) except Exception: # TOOD catch these raise if image is None: errors = ['URL is not a valid image'] raise ValidationError(errors) return image
def save_search_result(p, queue, retry=0): proxy = Proxy.get_random()['address'] url = SEARCH_URL.format(SEARCH_TEXT, p) try: r = fetch(url, proxy=proxy) except (Timeout, ConnectionError): sleep(0.1) retry += 1 if retry > 5: queue.put(url) raise GreenletExit() try: p = Proxy.objects.get(address=proxy) if p: p.delete() except DoesNotExist: pass return save_search_result(url, queue, retry) soup = BeautifulSoup(r.text, 'lxml') results = soup.find(class_='results') if results is None: # ???????, ?????? sleep(0.1) retry += 1 if retry > 5: queue.put(url) raise GreenletExit() return save_search_result(url, queue, retry) articles = results.find_all( 'div', lambda x: 'wx-rb' in x) for article in articles: save_article(article)
def check(auth_ref, args): # We call get_keystone_client here as there is some logic within to get a # new token if previous one is bad. keystone = get_keystone_client(auth_ref) auth_token = keystone.auth_token registry_endpoint = 'http://{ip}:9191'.format(ip=args.ip) s = Session() s.headers.update( {'Content-type': 'application/json', 'x-auth-token': auth_token}) try: # /images returns a list of public, non-deleted images r = s.get('%s/images' % registry_endpoint, verify=False, timeout=10) is_up = r.ok except (exc.ConnectionError, exc.HTTPError, exc.Timeout): is_up = False except Exception as e: status_err(str(e)) status_ok() metric_bool('glance_registry_local_status', is_up) # only want to send other metrics if api is up if is_up: milliseconds = r.elapsed.total_seconds() * 1000 metric('glance_registry_local_response_time', 'double', '%.3f' % milliseconds, 'ms')
def check(args): metadata_endpoint = ('http://{ip}:8775'.format(ip=args.ip)) is_up = True s = requests.Session() try: # looks like we can only get / (ec2 versions) without specifying # an instance ID and other headers versions = s.get('%s/' % metadata_endpoint, verify=False, timeout=10) milliseconds = versions.elapsed.total_seconds() * 1000 if not versions.ok or '1.0' not in versions.content.splitlines(): is_up = False except (exc.ConnectionError, exc.HTTPError, exc.Timeout) as e: is_up = False except Exception as e: status_err(str(e)) status_ok() metric_bool('nova_api_metadata_local_status', is_up) # only want to send other metrics if api is up if is_up: metric('nova_api_metadata_local_response_time', 'double', '%.3f' % milliseconds, 'ms')
def test_do_api_call_with_error_retry(self, mock_requests): for exception in [requests_exceptions.ConnectionError, requests_exceptions.Timeout]: with mock.patch.object(self.hook.log, 'error') as mock_errors: mock_requests.reset_mock() mock_requests.post.side_effect = exception() with self.assertRaises(AirflowException): self.hook._do_api_call(SUBMIT_RUN_ENDPOINT, {}) self.assertEquals(len(mock_errors.mock_calls), self.hook.retry_limit)
def test_get_unexpected_timeout(self): json_file = '%s/resources/test_function_single_get_unexpected_timeout.json' % self.current_dir_path test_function_list = self.generate_test_functions(json_file) with self.assertRaises(Timeout): run_test_function_list(test_function_list, self)