Python aiohttp.ClientConnectionError() Examples
The following are 30
code examples of aiohttp.ClientConnectionError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aiohttp
, or try the search function
.
Example #1
Source File: custom_puppet.py From mautrix-python with Mozilla Public License 2.0 | 6 votes |
def _sync(self) -> None: if not self.is_real_user: self.log.warning("Called sync() for non-custom puppet.") return custom_mxid: UserID = self.custom_mxid access_token_at_start: str = self.access_token errors: int = 0 filter_id: FilterID = await self._create_sync_filter() self.log.debug(f"Starting syncer for {custom_mxid} with sync filter {filter_id}.") while access_token_at_start == self.access_token: try: cur_batch = self.next_batch sync_resp = await self.intent.sync(filter_id=filter_id, since=cur_batch, set_presence=PresenceState.OFFLINE) self.next_batch = sync_resp.get("next_batch", None) errors = 0 if cur_batch is not None: self._handle_sync(sync_resp) except (MatrixError, ClientConnectionError, asyncio.TimeoutError) as e: errors += 1 wait = min(errors, 11) ** 2 self.log.warning(f"Syncer for {custom_mxid} errored: {e}. " f"Waiting for {wait} seconds...") await asyncio.sleep(wait) self.log.debug(f"Syncer for custom puppet {custom_mxid} stopped.")
Example #2
Source File: request.py From web3-gear with MIT License | 6 votes |
def make_request(self, method, params=None, data=None, **kwargs): headers = { "accept": "application/json", "Connection": "keep-alive", "Content-Type": "application/json" } kwargs.setdefault('headers', headers) kwargs.setdefault('timeout', 10) error = None try: response = await method(self._endpoint, params=params, data=data, **kwargs) return await response.json() except aiohttp.ClientConnectionError as e: print("Unable to connect to Thor-Restful server:") error = e except Exception as e: try: text = await response.text() error = Exception(text.strip('\n')) except: error = e print("Thor-Restful server Err:") raise error
Example #3
Source File: mock_server.py From aiobotocore with Apache License 2.0 | 6 votes |
def _wait_until_up(self): async with aiohttp.ClientSession() as session: for i in range(0, 30): if self.exitcode is not None: pytest.fail('unable to start/connect to aiohttp server') return try: # we need to bypass the proxies due to monkey patches await session.get(self.endpoint_url + '/ok', timeout=0.5) return except (aiohttp.ClientConnectionError, asyncio.TimeoutError): await asyncio.sleep(0.5) except BaseException: pytest.fail('unable to start/connect to aiohttp server') raise pytest.fail('unable to start and connect to aiohttp server')
Example #4
Source File: moto_server.py From aiobotocore with Apache License 2.0 | 6 votes |
def _start(self): self._thread = threading.Thread(target=self._server_entry, daemon=True) self._thread.start() async with aiohttp.ClientSession() as session: start = time.time() while time.time() - start < 10: if not self._thread.is_alive(): break try: # we need to bypass the proxies due to monkeypatches async with session.get(self.endpoint_url + '/static', timeout=_CONNECT_TIMEOUT): pass break except (asyncio.TimeoutError, aiohttp.ClientConnectionError): await asyncio.sleep(0.5) else: await self._stop() # pytest.fail doesn't call stop_process raise Exception("Can not start service: {}".format( self._service_name))
Example #5
Source File: aiohttp_client.py From py-stellar-base with Apache License 2.0 | 6 votes |
def post(self, url: str, data: Dict[str, str] = None) -> Response: """Perform HTTP POST request. :param url: the request url :param data: the data send to server :return: the response from server :raise: :exc:`ConnectionError <stellar_sdk.exceptions.ConnectionError>` """ try: response = await self._session.post(url, data=data, timeout=aiohttp.ClientTimeout(total=self.post_timeout)) return Response( status_code=response.status, text=await response.text(), headers=dict(response.headers), url=str(response.url), ) except aiohttp.ClientConnectionError as e: raise ConnectionError(e)
Example #6
Source File: multiplexed.py From aiodocker with Apache License 2.0 | 6 votes |
def fetch(self): while True: try: hdrlen = constants.STREAM_HEADER_SIZE_BYTES header = yield from self._response.content.readexactly(hdrlen) _, length = struct.unpack(">BxxxL", header) if not length: continue data = yield from self._response.content.readexactly(length) except ( aiohttp.ClientConnectionError, aiohttp.ServerDisconnectedError, asyncio.IncompleteReadError, ): break return data
Example #7
Source File: test_client.py From aiohttp-json-rpc with Apache License 2.0 | 6 votes |
def test_client_connection_failure(rpc_context, unused_tcp_port_factory): client = JsonRpcClient( url='ws://{host}:{port}{url}'.format( host=rpc_context.host, port=rpc_context.port, url=rpc_context.url, ) ) with pytest.raises(aiohttp.ClientConnectionError): await client.connect_url( 'ws://{host}:{port}{url}'.format( host=rpc_context.host, port=unused_tcp_port_factory(), url=rpc_context.url, ) ) assert client._session.closed is True
Example #8
Source File: slave.py From paasta with Apache License 2.0 | 6 votes |
def fetch(self, url, **kwargs) -> aiohttp.ClientResponse: headers = {"User-Agent": get_user_agent()} async with aiohttp.ClientSession( conn_timeout=self.config["response_timeout"], read_timeout=self.config["response_timeout"], ) as session: try: async with session.get( urljoin(self.host, url), headers=headers, **kwargs ) as response: await response.text() return response except aiohttp.ClientConnectionError: raise exceptions.SlaveDoesNotExist( f"Unable to connect to the slave at {self.host}" )
Example #9
Source File: cli.py From lbry-sdk with MIT License | 5 votes |
def execute_command(conf, method, params, callback=display): async with aiohttp.ClientSession() as session: try: message = {'method': method, 'params': params} async with session.get(conf.api_connection_url, json=message) as resp: try: data = await resp.json() if 'result' in data: return callback(data['result']) elif 'error' in data: return callback(data['error']) except Exception as e: log.exception('Could not process response from server:', exc_info=e) except aiohttp.ClientConnectionError: print("Could not connect to daemon. Are you sure it's running?")
Example #10
Source File: connection.py From homematicip-rest-api with GNU General Public License v3.0 | 5 votes |
def api_call(self, path, body=None, full_url=False): """Make the actual call to the HMIP server. Throws `HmipWrongHttpStatusError` or `HmipConnectionError` if connection has failed or response is not correct.""" result = None if not full_url: path = self.full_url(path) for i in range(self._restCallRequestCounter): try: with async_timeout.timeout(self._restCallTimout, loop=self._loop): result = await self._websession.post( path, data=body, headers=self.headers ) if result.status == 200: if result.content_type == "application/json": ret = await result.json() else: ret = True return ret else: raise HmipWrongHttpStatusError(result.status) except (asyncio.TimeoutError, aiohttp.ClientConnectionError): # Both exceptions occur when connecting to the server does # somehow not work. logger.debug("Connection timed out or another error occurred %s" % path) except JSONDecodeError as err: logger.exception(err) finally: if result is not None: await result.release() raise HmipConnectionError("Failed to connect to HomeMaticIp server")
Example #11
Source File: test_clients.py From uplink with MIT License | 5 votes |
def test_exceptions(self): import aiohttp exceptions = aiohttp_.AiohttpClient.exceptions with pytest.raises(exceptions.BaseClientException): raise aiohttp.ClientError() with pytest.raises(exceptions.BaseClientException): # Test polymorphism raise aiohttp.InvalidURL("invalid") with pytest.raises(exceptions.ConnectionError): raise aiohttp.ClientConnectionError() with pytest.raises(exceptions.ConnectionTimeout): raise aiohttp.ClientConnectorError.__new__( aiohttp.ClientConnectorError ) with pytest.raises(exceptions.ServerTimeout): raise aiohttp.ServerTimeoutError() with pytest.raises(exceptions.SSLError): raise aiohttp.ClientSSLError.__new__(aiohttp.ClientSSLError) with pytest.raises(exceptions.InvalidURL): raise aiohttp.InvalidURL("invalid")
Example #12
Source File: handlers.py From jupyter-server-proxy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _http_ready_func(self, p): url = 'http://localhost:{}'.format(self.port) async with aiohttp.ClientSession() as session: try: async with session.get(url) as resp: # We only care if we get back *any* response, not just 200 # If there's an error response, that can be shown directly to the user self.log.debug('Got code {} back from {}'.format(resp.status, url)) return True except aiohttp.ClientConnectionError: self.log.debug('Connection to {} refused'.format(url)) return False
Example #13
Source File: jsonstream.py From aiodocker with Apache License 2.0 | 5 votes |
def __anext__(self): while True: try: data = yield from self._response.content.readline() if not data: break except (aiohttp.ClientConnectionError, aiohttp.ServerDisconnectedError): break return self._transform(json.loads(data.decode("utf8"))) raise StopAsyncIteration
Example #14
Source File: logs.py From aiodocker with Apache License 2.0 | 5 votes |
def run(self, **params): if self.response: warnings.warn("already running", RuntimeWarning, stackelevel=2) return forced_params = {"follow": True} default_params = {"stdout": True, "stderr": True} params = ChainMap(forced_params, params, default_params) try: self.response = await self.docker._query( "containers/{self.container._id}/logs".format(self=self), params=params ) while True: msg = await self.response.content.readline() if not msg: break await self.channel.publish(msg) except (aiohttp.ClientConnectionError, aiohttp.ServerDisconnectedError): pass finally: # signal termination to subscribers await self.channel.publish(None) try: await self.response.release() except Exception: pass self.response = None
Example #15
Source File: test_chronos_scheduled_jobs_backend.py From asgard-api with MIT License | 5 votes |
def test_get_job_by_id_service_unavailable(self): """ Por enquanto deixamos o erro ser propagado. """ get_job_by_id_mock = CoroutineMock( side_effect=aiohttp.ClientConnectionError() ) self.backend.client = CoroutineMock(spec=ChronosClient) self.backend.client.get_job_by_id = get_job_by_id_mock user = User(**USER_WITH_MULTIPLE_ACCOUNTS_DICT) account = Account(**ACCOUNT_DEV_DICT) with self.assertRaises(aiohttp.ClientConnectionError): await self.backend.get_job_by_id("job-id", user, account)
Example #16
Source File: blivedm.py From blivedm with MIT License | 5 votes |
def init_room(self): try: async with self._session.get(ROOM_INIT_URL, params={'id': self._tmp_room_id}, ssl=self._ssl) as res: if res.status != 200: logger.warning('room %d room_init失败:%d %s', self._tmp_room_id, res.status, res.reason) return False data = await res.json() if data['code'] != 0: logger.warning('room %d room_init失败:%s', self._tmp_room_id, data['msg']) return False if not self._parse_room_init(data['data']): return False except aiohttp.ClientConnectionError: logger.exception('room %d room_init失败:', self._tmp_room_id) return False try: async with self._session.get(DANMAKU_SERVER_CONF_URL, params={'room_id': self._tmp_room_id}, ssl=self._ssl) as res: if res.status != 200: logger.warning('room %d getConf失败:%d %s', self._tmp_room_id, res.status, res.reason) return False data = await res.json() if data['code'] != 0: logger.warning('room %d getConf失败:%s', self._tmp_room_id, data['msg']) return False self._host_server_list = data['data']['host_server_list'] self._host_server_token = data['data']['token'] if not self._host_server_list: logger.warning('room %d getConf失败:host_server_list为空') return False except aiohttp.ClientConnectionError: logger.exception('room %d getConf失败:', self._tmp_room_id) return False return True
Example #17
Source File: blivedm.py From blivedm with MIT License | 5 votes |
def _heartbeat_loop(self): while True: try: await self._websocket.send_bytes(self._make_packet({}, Operation.HEARTBEAT)) await asyncio.sleep(self._heartbeat_interval) except (asyncio.CancelledError, aiohttp.ClientConnectionError): break
Example #18
Source File: user.py From vkbottle with MIT License | 5 votes |
def run(self, wait: int = DEFAULT_WAIT): """ Run user polling forever Can be manually stopped with: >> user.stop() """ self.wait = wait logger.info("Polling will be started. Is it OK?") await self.get_server() await self.on.dispatch() self.middleware.add_middleware(self.on.pre_p) self.status.dispatched = True logger.debug("User Polling successfully started") while not self._stop: try: event = await self.make_long_request(self.long_poll_server) if isinstance(event, dict) and event.get("ts"): self.loop.create_task(self.emulate(event)) self.long_poll_server["ts"] = event["ts"] else: await self.get_server() except ( aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError, TimeoutError, ): # No internet connection logger.warning("Server Timeout Error!") except: logger.error( "While user lp was running error occurred \n\n{}".format( traceback.format_exc() ) ) logger.error("Polling was stopped")
Example #19
Source File: http.py From Galaxy_Plugin_Bethesda with MIT License | 5 votes |
def handle_exception(): """ Context manager translating network related exceptions to custom :mod:`~galaxy.api.errors`. """ try: yield except asyncio.TimeoutError: raise BackendTimeout() except aiohttp.ServerDisconnectedError: raise BackendNotAvailable() except aiohttp.ClientConnectionError: raise NetworkError() except aiohttp.ContentTypeError: raise UnknownBackendResponse() except aiohttp.ClientResponseError as error: if error.status == HTTPStatus.UNAUTHORIZED: raise AuthenticationRequired() if error.status == HTTPStatus.FORBIDDEN: raise AccessDenied() if error.status == HTTPStatus.SERVICE_UNAVAILABLE: raise BackendNotAvailable() if error.status == HTTPStatus.TOO_MANY_REQUESTS: raise TooManyRequests() if error.status >= 500: raise BackendError() if error.status >= 400: logging.warning( "Got status %d while performing %s request for %s", error.status, error.request_info.method, str(error.request_info.url) ) raise UnknownError() except aiohttp.ClientError: logging.exception("Caught exception while performing request") raise UnknownError()
Example #20
Source File: client.py From rssant with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _group_send(self, dst_url, messages): with self._sentry_group_message_scope(dst_url): LOG.info(f'send {len(messages)} messages to {dst_url}') data = ActorMessage.batch_encode(messages, self.content_encoding) try: async with self.session.post(dst_url, data=data, headers=self.headers) as r: await r.read() except aiohttp.ClientConnectionError as ex: LOG.warning(f'failed to send message to {dst_url}: {ex}') return except aiohttp.ClientError as ex: LOG.warning(f'failed to send message to {dst_url}: {ex}') raise aiohttp_raise_for_status(r)
Example #21
Source File: http.py From galaxy-integrations-python-api with MIT License | 5 votes |
def handle_exception(): """ Context manager translating network related exceptions to custom :mod:`~galaxy.api.errors`. """ try: yield except asyncio.TimeoutError: raise BackendTimeout() except aiohttp.ServerDisconnectedError: raise BackendNotAvailable() except aiohttp.ClientConnectionError: raise NetworkError() except aiohttp.ContentTypeError as error: raise UnknownBackendResponse(error.message) except aiohttp.ClientResponseError as error: if error.status == HTTPStatus.UNAUTHORIZED: raise AuthenticationRequired(error.message) if error.status == HTTPStatus.FORBIDDEN: raise AccessDenied(error.message) if error.status == HTTPStatus.SERVICE_UNAVAILABLE: raise BackendNotAvailable(error.message) if error.status == HTTPStatus.TOO_MANY_REQUESTS: raise TooManyRequests(error.message) if error.status >= 500: raise BackendError(error.message) if error.status >= 400: logger.warning( "Got status %d while performing %s request for %s", error.status, error.request_info.method, str(error.request_info.url) ) raise UnknownError(error.message) except aiohttp.ClientError as e: logger.exception("Caught exception while performing request") raise UnknownError(repr(e))
Example #22
Source File: api.py From telepot with MIT License | 5 votes |
def request(req, **user_kw): fn, args, kwargs, timeout, cleanup = _transform(req, **user_kw) kwargs.update(_proxy_kwargs()) try: if timeout is None: async with fn(*args, **kwargs) as r: return await _parse(r) else: try: with async_timeout.timeout(timeout): async with fn(*args, **kwargs) as r: return await _parse(r) except asyncio.TimeoutError: raise exception.TelegramError('Response timeout', 504, {}) except aiohttp.ClientConnectionError: raise exception.TelegramError('Connection Error', 400, {}) finally: if cleanup: # e.g. closing one-time session if asyncio.iscoroutinefunction(cleanup): await cleanup() else: cleanup()
Example #23
Source File: url_checker_async.py From msticpy with MIT License | 5 votes |
def _check_url_async(url: str, session: ClientSession) -> UrlResult: """ Connect to URL and return response status. Parameters ---------- url : str URL to check session : ClientSession aiohttp client session Returns ------- UrlResult Tuple of status code, redirect history, requested url, status/error message. """ try: async with session.get(url) as resp: try: await resp.read() if resp.history: result = UrlResult( resp.status, resp.history, url, "No error. Redirect to " + str(resp.url), ) elif resp.status == 200: result = UrlResult( resp.status, resp.history, url, "No error. No redirect." ) else: result = UrlResult(resp.status, resp.history, url, "Error?") except ClientResponseError as client_err: return UrlResult(client_err.status, [], url, client_err) except ClientConnectionError as err: result = UrlResult(404, [], url, err) return result
Example #24
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def test_stream_reconnection_client_connection_error(): async with Stream() as stream: async def client_connection_error(): raise aiohttp.ClientConnectionError with patch.object(stream, '_connect', side_effect=stream_content): data = await stream.__anext__() assert 'connected' in data with patch.object(stream.response, 'readline', side_effect=client_connection_error): data = await stream.__anext__() assert data == {'reconnecting_in': ERROR_TIMEOUT, 'error': None}
Example #25
Source File: endpoint.py From lambda-text-extractor with Apache License 2.0 | 4 votes |
def _get_response(self, request, operation_model, attempts): # This will return a tuple of (success_response, exception) # and success_response is itself a tuple of # (http_response, parsed_dict). # If an exception occurs then the success_response is None. # If no exception occurs then exception is None. try: # http request substituted too async one logger.debug("Sending http request: %s", request) resp = yield from self._request( request.method, request.url, request.headers, request.body) http_response = resp except aiohttp.ClientConnectionError as e: e.request = request # botocore expects the request property # For a connection error, if it looks like it's a DNS # lookup issue, 99% of the time this is due to a misconfigured # region/endpoint so we'll raise a more specific error message # to help users. logger.debug("ConnectionError received when sending HTTP request.", exc_info=True) if self._looks_like_dns_error(e): better_exception = EndpointConnectionError( endpoint_url=request.url, error=e) return None, better_exception else: return None, e except aiohttp.http_exceptions.BadStatusLine: better_exception = ConnectionClosedError( endpoint_url=request.url, request=request) return None, better_exception except Exception as e: logger.debug("Exception received when sending HTTP request.", exc_info=True) return None, e # This returns the http_response and the parsed_data. response_dict = yield from convert_to_response_dict(http_response, operation_model) parser = self._response_parser_factory.create_parser( operation_model.metadata['protocol']) parsed_response = parser.parse( response_dict, operation_model.output_shape) return (http_response, parsed_response), None
Example #26
Source File: endpoint.py From lambda-text-extractor with Apache License 2.0 | 4 votes |
def _get_response(self, request, operation_model, attempts): # This will return a tuple of (success_response, exception) # and success_response is itself a tuple of # (http_response, parsed_dict). # If an exception occurs then the success_response is None. # If no exception occurs then exception is None. try: # http request substituted too async one logger.debug("Sending http request: %s", request) resp = yield from self._request( request.method, request.url, request.headers, request.body) http_response = resp except aiohttp.ClientConnectionError as e: e.request = request # botocore expects the request property # For a connection error, if it looks like it's a DNS # lookup issue, 99% of the time this is due to a misconfigured # region/endpoint so we'll raise a more specific error message # to help users. logger.debug("ConnectionError received when sending HTTP request.", exc_info=True) if self._looks_like_dns_error(e): better_exception = EndpointConnectionError( endpoint_url=request.url, error=e) return None, better_exception else: return None, e except aiohttp.http_exceptions.BadStatusLine: better_exception = ConnectionClosedError( endpoint_url=request.url, request=request) return None, better_exception except Exception as e: logger.debug("Exception received when sending HTTP request.", exc_info=True) return None, e # This returns the http_response and the parsed_data. response_dict = yield from convert_to_response_dict(http_response, operation_model) parser = self._response_parser_factory.create_parser( operation_model.metadata['protocol']) parsed_response = parser.parse( response_dict, operation_model.output_shape) return (http_response, parsed_response), None
Example #27
Source File: watching.py From kopf with MIT License | 4 votes |
def watch_objs( *, settings: configuration.OperatorSettings, resource: resources.Resource, namespace: Optional[str] = None, timeout: Optional[float] = None, since: Optional[str] = None, context: Optional[auth.APIContext] = None, # injected by the decorator freeze_waiter: asyncio_Future, ) -> AsyncIterator[bodies.RawInput]: """ Watch objects of a specific resource type. The cluster-scoped call is used in two cases: * The resource itself is cluster-scoped, and namespacing makes not sense. * The operator serves all namespaces for the namespaced custom resource. Otherwise, the namespace-scoped call is used: * The resource is namespace-scoped AND operator is namespaced-restricted. """ if context is None: raise RuntimeError("API instance is not injected by the decorator.") is_namespaced = await discovery.is_namespaced(resource=resource, context=context) namespace = namespace if is_namespaced else None params: Dict[str, str] = {} params['watch'] = 'true' if since is not None: params['resourceVersion'] = since if timeout is not None: params['timeoutSeconds'] = str(timeout) # Talk to the API and initiate a streaming response. response = await context.session.get( url=resource.get_url(server=context.server, namespace=namespace, params=params), timeout=aiohttp.ClientTimeout( total=settings.watching.client_timeout, sock_connect=settings.watching.connect_timeout, ), ) response.raise_for_status() # Stream the parsed events from the response until it is closed server-side, # or until it is closed client-side by the freeze-waiting future's callbacks. response_close_callback = lambda _: response.close() freeze_waiter.add_done_callback(response_close_callback) try: async with response: async for line in _iter_jsonlines(response.content): raw_input = cast(bodies.RawInput, json.loads(line.decode("utf-8"))) yield raw_input except (aiohttp.ClientConnectionError, aiohttp.ClientPayloadError): pass finally: freeze_waiter.remove_done_callback(response_close_callback)
Example #28
Source File: scrapetable.py From cjworkbench with GNU Affero General Public License v3.0 | 4 votes |
def spooled_data_from_url( url: str, headers: Dict[str, str] = {}, timeout: aiohttp.ClientTimeout = None, *, ssl: Optional[ssl.SSLContext] = None, ): """ Download `url` to a tempfile and yield `(bytesio, headers, charset)`. `bytesio` is backed by a temporary file: the file at path `bytesio.name` will exist within this context. Raise aiohttp.ClientError on generic error. Subclasses of note: * aiohttp.InvalidURL on invalid URL * aiohttp.ClientResponseError when HTTP status is not 200 * aiohttp.ClientPayloadError when server closes connection prematurely * aiohttp.ClientConnectionError (OSError) when connection fails Raise asyncio.TimeoutError when `timeout` seconds have expired. """ # aiohttp internally performs URL canonization before sending # request. DISABLE THIS: it breaks oauth and user's expectations. # # https://github.com/aio-libs/aiohttp/issues/3424 url = yarl.URL(url, encoded=True) # prevent magic if url.scheme not in ("http", "https"): raise aiohttp.InvalidURL("URL must start with http:// or https://") with tempfile_context(prefix="loadurl") as spool_path: async with aiohttp.ClientSession() as session: # raise aiohttp.ClientError, asyncio.TimeoutError async with session.get( url, headers=headers, timeout=timeout, ssl=ssl ) as response: # raise aiohttp.ClientResponseError response.raise_for_status() headers = response.headers charset = response.charset with spool_path.open("wb") as spool: # raise aiohttp.ClientPayloadError async for blob in response.content.iter_chunked(_ChunkSize): spool.write(blob) yield spool_path.open("rb"), headers, charset # dependency-injection, so unit tests can mock our functions
Example #29
Source File: wallet_server_monitor.py From lbry-sdk with MIT License | 4 votes |
def monitor(db, server): c = db.cursor() delay = 30 height_changed = None, time() height_change_reported = False first_attempt = True while True: try: async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(10)) as session: try: ws = await session.ws_connect(server) except (aiohttp.ClientConnectionError, asyncio.TimeoutError): if first_attempt: print(f"failed connecting to {server}") await boris_says(random.choice([ f"{server} is not responding, probably dead, will not connect again.", ])) return raise if first_attempt: await boris_says(f"{server} is online") else: await boris_says(f"{server} is back online") delay = 30 first_attempt = False print(f"connected to {server}") async for msg in ws: event = json.loads(msg.data) height = event['status'].get('height') height_change_time = int(time()-height_changed[1]) if height is None: pass elif height_changed[0] != height: height_changed = (height, time()) if height_change_reported: await boris_says( f"Server {server} received new block after {height_change_time / 60:.1f} minutes.", ) height_change_reported = False elif height_change_time > 30*60: if not height_change_reported or height_change_time % (2*60) == 0: await boris_says( f"It's been {height_change_time/60:.1f} minutes since {server} received a new block.", ) height_change_reported = True await handle_analytics_event(c, event, server) db.commit() except (aiohttp.ClientConnectionError, asyncio.TimeoutError): await boris_says(random.choice([ f"<!channel> Guys, we have a problem! Nobody home at {server}. Will check on it again in {delay} seconds.", f"<!channel> Something wrong with {server}. I think dead. Will poke it again in {delay} seconds.", f"<!channel> Don't hear anything from {server}, maybe dead. Will try it again in {delay} seconds.", ])) await asyncio.sleep(delay) delay += 30
Example #30
Source File: device.py From python-songpal with GNU General Public License v3.0 | 4 votes |
def create_post_request(self, method: str, params: Dict = None): """Call the given method over POST. :param method: Name of the method :param params: dict of parameters :return: JSON object """ if params is None: params = {} headers = {"Content-Type": "application/json"} payload = { "method": method, "params": [params], "id": next(self.idgen), "version": "1.0", } if self.debug > 1: _LOGGER.debug("> POST %s with body: %s", self.guide_endpoint, payload) try: async with aiohttp.ClientSession(headers=headers) as session: res = await session.post( self.guide_endpoint, json=payload, headers=headers ) if self.debug > 1: _LOGGER.debug("Received %s: %s" % (res.status, res.text)) if res.status != 200: res_json = await res.json(content_type=None) raise SongpalException( "Got a non-ok (status %s) response for %s" % (res.status, method), error=res_json.get("error"), ) res_json = await res.json(content_type=None) except (aiohttp.InvalidURL, aiohttp.ClientConnectionError) as ex: raise SongpalException("Unable to do POST request: %s" % ex) from ex if "error" in res_json: raise SongpalException( "Got an error for %s" % method, error=res_json["error"] ) if self.debug > 1: _LOGGER.debug("Got %s: %s", method, pf(res_json)) return res_json