我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用requests.exceptions.ConnectionError()。
def fetch_history(self): if self.current_account is None: return address = '0x' + self.current_account.address.encode("hex") try: transactions = PyWalib.get_transaction_history(address) except ConnectionError: Controller.on_history_connection_error() Logger.warning('ConnectionError', exc_info=True) return except NoTransactionFoundException: transactions = [] except ValueError: # most likely the JSON object could not be decoded, refs #91 Controller.on_history_value_error() # currently logged as an error, because we want more insight # in order to eventually handle it more specifically Logger.error('ValueError', exc_info=True) return # triggers accounts_history observers update self.controller.accounts_history[address] = transactions
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 test_remote_update_lambda(self): response = self.deploy.remote_get_lambda(**self.lambda_conf) self.assertFalse(response) self._create_lambda_from_s3() response = self.deploy.remote_get_lambda(**self.lambda_conf) self.assertEqual(response["ResponseMetadata"]["HTTPStatusCode"], 200) for key in ["FunctionName", "Role", "Description", "Runtime", "Handler"]: self.assertEqual(response["Configuration"][key], self.lambda_conf[key]) lambda_new_conf = { "FunctionName": "LambdaExample1", "Description": "string2", "Runtime": "python3.6", } # TODO: research this error from MOTO try: response = self.deploy.remote_update_conf_lambada(**self.lambda_conf) self.assertEqual(response["ResponseMetadata"]["HTTPStatusCode"], 200) for key in ["FunctionName", "Role", "Description", "Runtime", "Handler"]: self.assertEqual(response["Configuration"][key], lambda_new_conf[key]) except ConnectionError: pass
def test_run_with_alias(self, create_artefact_mock, copytree_mock, pip_install_mock): zip_file = MockZipFile.create_zip("test") create_artefact_mock.return_value = zip_file self.deploy = Deploy(path=os.path.dirname(os.path.abspath(__file__)), filename="config_with_alias.json") # TODO: Search why moto rise errors try: # Create lambdas self.deploy.run("myexamplelambdaproject") self.assertTrue(pip_install_mock.called) self.assertTrue(copytree_mock.called) self.assertTrue(create_artefact_mock.called) # Update lambdas self.deploy.run("myexamplelambdaproject") except ConnectionError as e: print(e) os.remove(zip_file)
def test_run_with_trigger_s3(self, create_artefact_mock, copytree_mock, pip_install_mock): zip_file = MockZipFile.create_zip("test") create_artefact_mock.return_value = zip_file self.deploy = Deploy(path=os.path.dirname(os.path.abspath(__file__)), filename="config_with_triggers.json", lambdas_to_deploy=["LambdaExample_S3_7", ]) # TODO: Search why moto rise errors try: # Create lambdas self.deploy.run("myexamplelambdaproject") self.assertTrue(pip_install_mock.called) self.assertTrue(copytree_mock.called) self.assertTrue(create_artefact_mock.called) # Update lambdas self.deploy.run("myexamplelambdaproject") except ConnectionError as e: print(e) os.remove(zip_file)
def test_run_with_trigger_cloudwatch(self, create_artefact_mock, copytree_mock, pip_install_mock): zip_file = MockZipFile.create_zip("test") create_artefact_mock.return_value = zip_file self.deploy = Deploy(path=os.path.dirname(os.path.abspath(__file__)), filename="config_with_triggers.json", lambdas_to_deploy=["LambdaExample_CWE_9", ]) try: # Create lambdas self.deploy.run("myexamplelambdaproject") self.assertTrue(pip_install_mock.called) self.assertTrue(copytree_mock.called) self.assertTrue(create_artefact_mock.called) # Update lambdas self.deploy.run("myexamplelambdaproject") except ConnectionError as e: print(e) os.remove(zip_file)
def execute(self): try: self.system.run() except (ReadTimeout, ConnectionError, JSONDecodeError): pass except exceptions.TradingSystemException as e: curr = datetime.now() print('{time} - {text}'.format(time=curr.strftime('%Y-%m-%d %H:%M:%S'), text=str(e))) except Exception as e: curr = datetime.now() print('{time} - {text} - {args}'.format(time=curr.strftime('%Y-%m-%d %H:%M:%S'), text=str(e), args=e.args)) traceback.print_exc() if self.interval: threading.Timer(self.interval, self.execute).start()
def test_webhook_to_project_conn_err(self, mock): """Test WEB update does not set a webhook for the project""" from requests.exceptions import ConnectionError mock.side_effect = ConnectionError self.register() owner = db.session.query(User).first() project = ProjectFactory.create(owner=owner) new_webhook = 'http://mynewserver.com/' res = self.update_project(id=project.id, short_name=project.short_name, new_webhook=new_webhook) err_msg = "There should not be an updated webhook url." assert project.webhook != new_webhook, err_msg
def test_downloader_conn_error(self): exception = ConnectionError() with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps: max_retry = 3 for _ in range(max_retry + 1): rsps.add(responses.GET, self.TEST_MPD_URL, body=exception) dl = live.Downloader( mpd=self.TEST_MPD_URL, output_dir='output_connerror', duplicate_etag_retry=2, singlethreaded=True, max_connection_error_retry=max_retry) dl.run() dl.stream_id = '17875351285037717' output_file = 'output_connerror.mp4' dl.stitch(output_file, cleartempfiles=True) self.assertFalse(os.path.isfile(output_file), '{0!s} not generated'.format(output_file))
def run(self): """Executes tasks until the queue is empty""" while True: try: image = self.queue.get() for _ in six.moves.range(self.conf.retries + 1): self.builder(image) if image['status'] in ['built', 'unmatched', 'parent_error']: break except requests_exc.ConnectionError: LOG.exception('Make sure Docker is running and that you' ' have the correct privileges to run Docker' ' (root)') image['status'] = "connection_error" break self.end_task(image)
def delete_user_from_group(self,uname,gid): """ ??????????????????? """ uid = "" for user in self.group_members[gid]: if user['NickName'] == uname: uid = user['UserName'] if uid == "": return False url = self.base_uri + '/webwxupdatechatroom?fun=delmember&pass_ticket=%s' % self.pass_ticket params ={ "DelMemberList": uid, "ChatRoomName": gid, "BaseRequest": self.base_request } headers = {'content-type': 'application/json; charset=UTF-8'} data = json.dumps(params, ensure_ascii=False).encode('utf8') try: r = self.session.post(url, data=data, headers=headers) except (ConnectionError, ReadTimeout): return False dic = r.json() return dic['BaseResponse']['Ret'] == 0
def send_msg_by_uid(self, word, dst='filehelper'): url = self.base_uri + '/webwxsendmsg?pass_ticket=%s' % self.pass_ticket msg_id = str(int(time.time() * 1000)) + str(random.random())[:5].replace('.', '') word = self.to_unicode(word) params = { 'BaseRequest': self.base_request, 'Msg': { "Type": 1, "Content": word, "FromUserName": self.my_account['UserName'], "ToUserName": dst, "LocalID": msg_id, "ClientMsgId": msg_id } } headers = {'content-type': 'application/json; charset=UTF-8'} data = json.dumps(params, ensure_ascii=False).encode('utf8') try: r = self.session.post(url, data=data, headers=headers) except (ConnectionError, ReadTimeout): return False dic = r.json() return dic['BaseResponse']['Ret'] == 0
def get_authorization_url(self, callback_uri): session = OAuth1Session( settings.OAUTH_CONSUMER_KEY, client_secret=settings.OAUTH_CONSUMER_SECRET, callback_uri=callback_uri, ) try: url = settings.API_HOST + settings.OAUTH_TOKEN_PATH response = session.fetch_request_token(url) except (ValueError, TokenRequestDenied, ConnectionError) as err: raise AuthenticatorError(err) else: self.token = response.get('oauth_token') self.secret = response.get('oauth_token_secret') url = settings.API_HOST + settings.OAUTH_AUTHORIZATION_PATH authorization_url = session.authorization_url(url) LOGGER.log(logging.INFO, 'Initial token {}, secret {}'.format( self.token, self.secret)) return authorization_url
def set_access_token(self, authorization_url): session = OAuth1Session( settings.OAUTH_CONSUMER_KEY, settings.OAUTH_CONSUMER_SECRET, resource_owner_key=self.token, resource_owner_secret=self.secret, ) session.parse_authorization_response(authorization_url) url = settings.API_HOST + settings.OAUTH_ACCESS_TOKEN_PATH try: response = session.fetch_access_token(url) except (TokenRequestDenied, ConnectionError) as err: raise AuthenticatorError(err) else: self.token = response.get('oauth_token') self.secret = response.get('oauth_token_secret') LOGGER.log(logging.INFO, 'Updated token {}, secret {}'.format( self.token, self.secret))
def call(self, method='GET', url='', payload=None): """Workhorse which actually calls the API""" log(logging.INFO, '{} {}'.format(method, url)) if payload: log(logging.INFO, 'Payload: {}'.format(payload)) # use `requests` if no session has been started session = self.session or requests time_start = time.time() try: if payload: response = session.request(method, url, json=payload) else: response = session.request(method, url) except ConnectionError as err: raise APIError(err) time_end = time.time() elapsed = int((time_end - time_start) * 1000) log(logging.INFO, 'Took {} ms'.format(elapsed)) response.execution_time = elapsed return response
def login(self): """ ??????? :return: """ try: data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.app_id, 'appkey': self.app_key} response = requests.post(self.api_url, data=data) if response.status_code == 200: result = response.json() print(result) if 'ret' in result.keys() and result.get('ret') < 0: return self.error(result.get('ret')) else: return result return None except ConnectionError: return None
def upload(self, files, timeout, code_type): """ ??????????? :param files: :param timeout: :param code_type: :return: """ try: data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.app_id, 'appkey': self.app_key, 'codetype': str(code_type), 'timeout': str(timeout)} response = requests.post(self.api_url, data=data, files=files) if response.status_code == 200: return response.json() return None except ConnectionError: return None
def _login(self): try: request_url = '{0}{1}'.format(self._url(), self.auth_url) request_headers = copy.copy(HEADERS) request_headers['USERNAME'] = self.username request_headers['PASSWORD'] = self.password response = requests.post(request_url, headers=request_headers, verify=self.verify_cert) payload = response.json() if 'token' not in payload: raise FXOSApiException('Could not retrieve token from {0}.'.format(request_url)) if response.status_code == 400: if '551' in response.content: raise FXOSAuthException('FX-OS API Authentication to {0} failed.'.format(self.hostname)) if '552' in response.content: raise FXOSAuthException('FX-OS API Authorization to {0} failed'.format(self.hostname)) return payload['token'] except ConnectionError: self.logger.error( 'Could not connect to {0}. Max retries exceeded with url: {1}'.format(self.hostname, request_url)) except FXOSApiException as exc: self.logger.error(exc.message) except Exception as exc: self.logger.exception(exc.message)
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 get_page(url, options={}): """ ???? :param url: :param options: :return: """ headers = dict(base_headers, **options) print('????', url) try: response = requests.get(url, headers=headers) print('????', url, response.status_code) if response.status_code == 200: return response.text except ConnectionError: print('????', url) return None
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 test_connect(self): """ ????? Influxdb ???, ?? Influxdb ???? :return: None """ i = 0 while True: try: self.query("show measurements limit 1") return True except (Connectionerror, InfluxDBClientError, Exception) as e: i += 1 if i > 10: return False log.error(e) time.sleep(2)
def fetch_listing_images(client, listing): """ Makes use of some built-in rets-python methods to get image URLs for a listing. Note: this function (and get_listing_image()) were only necessary because rets-python failed to return anything useful with its Record.get_object() method when the location parameter was set to True. """ headers = {'Accept': '*/*'} payload = {'Resource': 'Property', 'Type': 'Photo', 'ID': _build_entity_object_ids( listing.resource_key), 'Location': 1} encoding = 'utf-8' try: response = client.http._http_post(client.http._url_for('GetObject'), headers=headers, payload=payload) except (ProtocolError, ConnectionError, ConnectionResetError): client.http.login() response = client.http._http_post(client.http._url_for('GetObject'), headers=headers, payload=payload) multipart = MultipartDecoder.from_response(response, encoding) return multipart.parts
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 delete_user_from_group(self, uname, gid): """ ??????????????????? """ uid = "" for user in self.group_members[gid]: if user['NickName'] == uname: uid = user['UserName'] if uid == "": return False url = self.base_uri + '/webwxupdatechatroom?fun=delmember&pass_ticket=%s' % self.pass_ticket params = { "DelMemberList": uid, "ChatRoomName": gid, "BaseRequest": self.base_request } headers = {'content-type': 'application/json; charset=UTF-8'} data = json.dumps(params, ensure_ascii=False).encode('utf8') try: r = self.session.post(url, data=data, headers=headers) except (ConnectionError, ReadTimeout): return False dic = r.json() return dic['BaseResponse']['Ret'] == 0
def test_retry(self): """Test retrying.""" self.http.post.side_effect = ( [exceptions.ConnectionError()] * common.RETRY_COUNT + ['Something']) self.assertEqual( 'Something', common.post( url='a', headers={'c': 'd'}, data={'e': 'f'}, random='thing')) self.assertTrue(os.path.exists(common.CLUSTERFUZZ_TESTCASES_DIR)) self.assertEqual(common.RETRY_COUNT + 1, self.mock.CachedSession.call_count) self.assertEqual(common.RETRY_COUNT + 1, self.http.mount.call_count) self.assert_exact_calls( self.http.post, [ mock.call( url='a', headers={'c': 'd'}, data={'e': 'f'}, random='thing') ] * (common.RETRY_COUNT + 1) )
def test_exception(self): """Test retrying.""" self.http.post.side_effect = ( [exceptions.ConnectionError()] * (common.RETRY_COUNT + 1)) with self.assertRaises(exceptions.ConnectionError): common.post( url='a', headers={'c': 'd'}, data={'e': 'f'}, random='thing') self.assertTrue(os.path.exists(common.CLUSTERFUZZ_TESTCASES_DIR)) self.assertEqual(common.RETRY_COUNT + 1, self.mock.CachedSession.call_count) self.assertEqual(common.RETRY_COUNT + 1, self.http.mount.call_count) self.assert_exact_calls( self.http.post, [ mock.call( url='a', headers={'c': 'd'}, data={'e': 'f'}, random='thing') ] * (common.RETRY_COUNT + 1) )
def get_retry_extended(): retry = urllib3.util.Retry(total=MAX_RETRIES, connect=MAX_RETRIES, read=MAX_RETRIES, backoff_factor=BACKOFF_FACTOR) def attempt(url, retry=retry): try: # this essentially creates a new connection pool per request :-( session = requests.Session() adapter = requests.adapters.HTTPAdapter(max_retries=retry) session.mount(RETRY_PREFIX, adapter) req = requests.Request('GET', url).prepare() # would be nice just to pass retry here, but we cannot :-( r = session.send(req, timeout=TIMEOUT) r.raise_for_status() # except MaxRetryError: # raise except ConnectionError as e: # increment() will return a new Retry() object retry = retry.increment(req.method, url, error=e) retry.sleep() logging.warning("Retrying (%r) after connection broken by '%r': '%s'", retry, e, url) return attempt(url, retry=retry) return r return attempt(URL).json()
def get_content_aware(): retry = urllib3.util.Retry(total=MAX_RETRIES, connect=MAX_RETRIES, read=MAX_RETRIES, backoff_factor=BACKOFF_FACTOR) def attempt(url, retry=retry): try: session = requests.Session() adapter = requests.adapters.HTTPAdapter(max_retries=retry) session.mount(RETRY_PREFIX, adapter) req = requests.Request('GET', url).prepare() r = session.send(req, timeout=TIMEOUT) r.raise_for_status() j = r.json() # DEMO ONLY. TypeError is too wide to handle here except (ConnectionError, TypeError) as e: retry = retry.increment(req.method, url, error=e) retry.sleep() logging.warning("Retrying (%r) after connection broken by '%r': '%s'", retry, e, url) return attempt(url, retry=retry) return j return attempt(URL)
def get_page(url, options={}): ua = UserAgent() base_headers = { 'User-Agent': ua.random, 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8' } headers = dict(base_headers, **options) print('Getting', url) try: r = requests.get(url, headers=headers) print('Getting result', url, r.status_code) if r.status_code == 200: return r.text except ConnectionError: print('Crawling Failed', url) return None
def makeRequest(url, logger, redirect=True, proxies={}): packages.urllib3.disable_warnings() try: response = get(url, allow_redirects=redirect, verify=False, proxies=proxies, timeout=25) newurl = "{}/{}".format(url, meta_redirect(response.text)) while newurl: response = get(newurl, allow_redirects=redirect, verify=False, proxies=proxies, timeout=25) newurl = meta_redirect(response.text) except exceptions.ConnectionError, e: logger.debug("[-] {} - {}".format(url, str(e))) return except exceptions.ReadTimeout, e: logger.debug("[-] {} - {}".format(url, str(e))) return else: return parseResponse(response)
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 ignoreConnectionErrors(*fun,**opts): def wrapper_decorator(f): @wraps(f) def wrapper(*args,**kwargs): while True: try: f(*args,**kwargs) break except ConnectionError as e: if opts.get('echo',False): print e time.sleep(1) return wrapper if len(fun) == 1: if callable(fun[0]): return wrapper_decorator(fun[0]) else: raise TypeError("argument 1 to @ignoreConnectionErrors has to be callable") if fun: raise TypeError("@ignoreConnectionErrors takes 1 argument, {0} given".format(sum([len(fun),len(opts)]))) return wrapper_decorator
def wait_for_software_install_on_device(self): # We need to delay this slightly in case the the volume needs to be # created first for _ in range(10): try: if self.volume_exists_on_device(): break except ConnectionError: pass time.sleep(5) progress = self.load_volume_on_device() while True: time.sleep(10) progress.refresh() status = progress.status if 'complete' in status: break elif 'failed' in status: raise F5ModuleError(status)
def test_if_cosmos_is_only_available_locally(dcos_api_session): # One should not be able to connect to the cosmos HTTP and admin ports # over non-lo interfaces msg = "Cosmos reachable from non-lo interface" with pytest.raises(ConnectionError, message=msg): dcos_api_session.get('/', host=dcos_api_session.masters[0], port=7070, scheme='http') with pytest.raises(ConnectionError, message=msg): dcos_api_session.get('/', host=dcos_api_session.masters[0], port=9990, scheme='http') # One should be able to connect to the cosmos HTTP and admin ports at # 127.0.0.1:7070 and 127.0.0.1:9990. # Getting HTTP error codes shows that we made it all the way to # cosmos which is exactly what we're testing. r = dcos_api_session.get('/', host="127.0.0.1", port=7070, scheme='http') assert r.status_code == 404 # In this case localhost:9990/ redirects to localhost:9990/admin so we # we expect a 200 r = dcos_api_session.get('/', host="127.0.0.1", port=9990, scheme='http') assert r.status_code == 200
def collect_usage(self): url = urlparse.urljoin(self.endpoint, "collect_usage") headers = {"Content-Type": "application/json", "X-Auth-Token": self.auth_token} try: response = requests.post(url, headers=headers, verify=not self.insecure) if response.status_code != 200: raise AttributeError("Usage cycle failed: %s code: %s" % (response.text, response.status_code)) else: return response.json() except ConnectionError as e: print(e)
def last_collected(self): url = urlparse.urljoin(self.endpoint, "last_collected") headers = {"Content-Type": "application/json", "X-Auth-Token": self.auth_token} try: response = requests.get(url, headers=headers, verify=not self.insecure) if response.status_code != 200: raise AttributeError("Get last collected failed: %s code: %s" % (response.text, response.status_code)) else: return response.json() except ConnectionError as e: print(e)
def _query_usage(self, tenant, start, end, endpoint): url = urlparse.urljoin(self.endpoint, endpoint) headers = {"X-Auth-Token": self.auth_token} params = {"tenant": tenant, "start": start, "end": end } try: response = requests.get(url, headers=headers, params=params, verify=not self.insecure) if response.status_code != 200: raise AttributeError("Get usage failed: %s code: %s" % (response.text, response.status_code)) else: return response.json() except ConnectionError as e: print(e)