Python websockets.exceptions.ConnectionClosed() Examples
The following are 29
code examples of websockets.exceptions.ConnectionClosed().
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
websockets.exceptions
, or try the search function
.
Example #1
Source File: adapters.py From jarvis with GNU General Public License v2.0 | 6 votes |
def _feed_buffer(self, n=1): """ Feed the data buffer by reading a Websocket message. :param n: if given, feed buffer until it contains at least n bytes """ buffer = bytearray(self._stream.read()) while len(buffer) < n: try: message = yield from self._protocol.recv() except ConnectionClosed: message = None if message is None: break if not isinstance(message, bytes): raise TypeError("message must be bytes") buffer.extend(message) self._stream = io.BytesIO(buffer)
Example #2
Source File: remote_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #3
Source File: bitcoin_com_websocket.py From hummingbot with Apache License 2.0 | 6 votes |
def _messages(self) -> AsyncIterable[Any]: try: while True: try: raw_msg_str: str = await asyncio.wait_for(self._client.recv(), timeout=self.MESSAGE_TIMEOUT) raw_msg = ujson.loads(raw_msg_str) yield raw_to_response(raw_msg) except asyncio.TimeoutError: try: pong_waiter = await self._client.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await self.disconnect() # emit messages
Example #4
Source File: dolomite_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #5
Source File: bitfinex_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _get_response(self, ws: websockets.WebSocketClientProtocol): try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) self._last_recv_time = time.time() yield msg except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #6
Source File: huobi_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #7
Source File: mock_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #8
Source File: binance_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #9
Source File: bamboo_relay_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #10
Source File: kucoin_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close() # get required data to create a websocket request
Example #11
Source File: kraken_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 6 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) if ((msg != "{\"event\":\"heartbeat\"}" and "\"event\":\"systemStatus\"" not in msg and "\"event\":\"subscriptionStatus\"" not in msg)): yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #12
Source File: adapters.py From hbmqtt with MIT License | 6 votes |
def _feed_buffer(self, n=1): """ Feed the data buffer by reading a Websocket message. :param n: if given, feed buffer until it contains at least n bytes """ buffer = bytearray(self._stream.read()) while len(buffer) < n: try: message = yield from self._protocol.recv() except ConnectionClosed: message = None if message is None: break if not isinstance(message, bytes): raise TypeError("message must be bytes") buffer.extend(message) self._stream = io.BytesIO(buffer)
Example #13
Source File: websockets.py From gql with MIT License | 6 votes |
def _receive(self) -> str: """Wait the next message from the websocket connection and log the answer """ # We should always have an active websocket connection here assert self.websocket is not None # Wait for the next websocket frame. Can raise ConnectionClosed data: Data = await self.websocket.recv() # websocket.recv() can return either str or bytes # In our case, we should receive only str here if not isinstance(data, str): raise TransportProtocolError("Binary data received in the websocket") answer: str = data log.info("<<< %s", answer) return answer
Example #14
Source File: network_options.py From shadowlands with MIT License | 6 votes |
def _attempt_connection(self, fn_name, arg=None): fn = self._interface.node.__getattribute__(fn_name) self._interface.node.thread_shutdown = True self._interface.node.heartbeat_thread.join() self._interface.node.thread_shutdown = False try: #debug(); pdb.set_trace() if arg: return fn(arg) else: return fn() except StaleBlockchain: self._scene.add_effect( MessageDialog(self._screen, "Stale blockchain on selected Node")) return #except (AttributeError, InvalidStatusCode, ConnectionClosed, TimeoutError, OSError) as e: #Timeout # self._scene.add_effect( MessageDialog(self._screen, "Could not connect to node ({})".format(str(e.__class__)))) # return self._interface.node.start_heartbeat_thread()
Example #15
Source File: generic_websocket.py From bitfinex-api-py with Apache License 2.0 | 5 votes |
def _run_socket(self): retries = 0 sId = len(self.sockets) s = Socket(sId) self.sockets[sId] = s loop = asyncio.get_event_loop() while retries < self.max_retries and self.attempt_retry: try: async with websockets.connect(self.host) as websocket: self.sockets[sId].set_websocket(websocket) self.sockets[sId].set_connected() self.logger.info("Websocket connected to {}".format(self.host)) retries = 0 while True: # optimization - wait 0 seconds to force the async queue # to be cleared before continuing await asyncio.sleep(0) message = await websocket.recv() await self.on_message(sId, message) except (ConnectionClosed, socket.error) as e: self.sockets[sId].set_disconnected() if self.sockets[sId].isAuthenticated: self.sockets[sId].set_unauthenticated() self._emit('disconnected') if (not self.attempt_retry): return self.logger.error(str(e)) retries += 1 # wait 5 seconds befor retrying self.logger.info("Waiting 5 seconds before retrying...") await asyncio.sleep(5) self.logger.info("Reconnect attempt {}/{}".format(retries, self.max_retries)) self.logger.info("Unable to connect to websocket.") self._emit('stopped')
Example #16
Source File: liquid_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: """ Generator function that returns messages from the web socket stream :param ws: current web socket connection :returns: message in AsyncIterable format """ # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=Constants.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=Constants.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #17
Source File: liquid_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: """ Generator function that returns messages from the web socket stream :param ws: current web socket connection :returns: message in AsyncIterable format """ # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=Constants.MESSAGE_TIMEOUT) self._last_recv_time = time.time() yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() self._last_recv_time = time.time() await asyncio.wait_for(pong_waiter, timeout=Constants.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #18
Source File: coinbase_pro_api_user_stream_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: """ Generator function that returns messages from the web socket stream :param ws: current web socket connection :returns: message in AsyncIterable format """ # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) self._last_recv_time = time.time() yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() self._last_recv_time = time.time() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #19
Source File: bitfinex_api_order_book_data_source.py From hummingbot with Apache License 2.0 | 5 votes |
def _get_response(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
Example #20
Source File: conftest.py From gql with MIT License | 5 votes |
def get_server_handler(request): """Get the server handler. Either get it from test or use the default server handler if the test provides only an array of answers. """ if isinstance(request.param, types.FunctionType): server_handler = request.param else: answers = request.param async def default_server_handler(ws, path): try: await WebSocketServer.send_connection_ack(ws) query_id = 1 for answer in answers: result = await ws.recv() print(f"Server received: {result}") if isinstance(answer, str) and "{query_id}" in answer: answer_format_params = {"query_id": query_id} formatted_answer = answer.format(**answer_format_params) else: formatted_answer = answer await ws.send(formatted_answer) await WebSocketServer.send_complete(ws, query_id) query_id += 1 await WebSocketServer.wait_connection_terminate(ws) await ws.wait_closed() except ConnectionClosed: pass server_handler = default_server_handler return server_handler
Example #21
Source File: prerender.py From chrome-prerender with MIT License | 5 votes |
def render(self, url: str, format: str = 'html', proxy: str = '') -> str: if not self._pages: raise RuntimeError('No browser available') try: page = await asyncio.wait_for(self._idle_pages.get(), timeout=10) except asyncio.TimeoutError: raise TemporaryBrowserFailure('No Chrome page available in 10s') reopen = False try: try: await page.attach(proxy) except asyncio.TimeoutError: logger.error('Attach to Chrome page %s timed out, page is likely closed', page.id) reopen = True raise TemporaryBrowserFailure('Attach to Chrome page timed out') data = await asyncio.wait_for(page.render(url, format), timeout=PRERENDER_TIMEOUT) return data except InvalidHandshake: logger.error('Chrome invalid handshake for page %s', page.id) reopen = True raise TemporaryBrowserFailure('Invalid handshake') except ConnectionClosed: logger.error('Chrome remote connection closed for page %s', page.id) reopen = True raise TemporaryBrowserFailure('Chrome remote debugging connection closed') except RuntimeError as e: # https://github.com/MagicStack/uvloop/issues/68 if 'unable to perform operation' in str(e): logger.error('RuntimeError: %s', str(e)) reopen = True raise TemporaryBrowserFailure(str(e)) else: raise finally: await asyncio.shield(self._manage_page(page, reopen))
Example #22
Source File: test_connection.py From python-libjuju with Apache License 2.0 | 5 votes |
def recv(self): if not self.responses: await asyncio.sleep(1) # delay to give test time to finish raise ConnectionClosed(0, 'ran out of responses') return json.dumps(self.responses.popleft())
Example #23
Source File: mee6.py From mee6 with MIT License | 5 votes |
def connect_schwifty(self): self.schwifty = await SchwiftyWebsocket.create( self.shard, self ) while not self.is_closed: try: await self.schwifty.poll_event() except (ConnectionClosed, asyncio.TimeoutError) as e: await asyncio.sleep(1) self.schwifty = await SchwiftyWebsocket.create( self.shard, self )
Example #24
Source File: test_chrome.py From chromewhip with MIT License | 5 votes |
def chrome_tab(): """Ensure Chrome is running """ browser = ChromeMock(host=TEST_HOST, port=TEST_PORT) await browser.connect() chrome_tab = browser.tabs[0] yield chrome_tab print("gracefully disconnecting chrome tab...") try: await chrome_tab.disconnect() except ConnectionClosed: pass
Example #25
Source File: main.py From Foundations-of-Blockchain with MIT License | 5 votes |
def p2p_handler(self, request, ws): logger.info('listening websocket p2p port on: %d' % port) try: await self.init_connection(ws) except (ConnectionClosed): await self.connection_closed(ws)
Example #26
Source File: websockets.py From gql with MIT License | 5 votes |
def _send(self, message: str) -> None: """Send the provided message to the websocket connection and log the message """ if not self.websocket: raise TransportClosed( "Transport is not connected" ) from self.close_exception try: await self.websocket.send(message) log.info(">>> %s", message) except ConnectionClosed as e: await self._fail(e, clean_close=False) raise e
Example #27
Source File: main.py From Foundations-of-Blockchain with MIT License | 5 votes |
def p2p_handler(self, request, ws): logger.info('listening websocket p2p port on: %d' % port) try: await self.init_connection(ws) except (ConnectionClosed): await self.connection_closed(ws)
Example #28
Source File: websockets.py From gql with MIT License | 4 votes |
def subscribe( self, document: DocumentNode, variable_values: Optional[Dict[str, str]] = None, operation_name: Optional[str] = None, send_stop: Optional[bool] = True, ) -> AsyncGenerator[ExecutionResult, None]: """Send a query and receive the results using a python async generator. The query can be a graphql query, mutation or subscription. The results are sent as an ExecutionResult object. """ # Send the query and receive the id query_id: int = await self._send_query( document, variable_values, operation_name ) # Create a queue to receive the answers for this query_id listener = ListenerQueue(query_id, send_stop=(send_stop is True)) self.listeners[query_id] = listener # We will need to wait at close for this query to clean properly self._no_more_listeners.clear() try: # Loop over the received answers while True: # Wait for the answer from the queue of this query_id # This can raise a TransportError or ConnectionClosed exception. answer_type, execution_result = await listener.get() # If the received answer contains data, # Then we will yield the results back as an ExecutionResult object if execution_result is not None: yield execution_result # If we receive a 'complete' answer from the server, # Then we will end this async generator output without errors elif answer_type == "complete": log.debug( f"Complete received for query {query_id} --> exit without error" ) break except (asyncio.CancelledError, GeneratorExit) as e: log.debug("Exception in subscribe: " + repr(e)) if listener.send_stop: await self._send_stop_message(query_id) listener.send_stop = False finally: del self.listeners[query_id] if len(self.listeners) == 0: self._no_more_listeners.set()
Example #29
Source File: websockets.py From gql with MIT License | 4 votes |
def _receive_data_loop(self) -> None: try: while True: # Wait the next answer from the websocket server try: answer = await self._receive() except (ConnectionClosed, TransportProtocolError) as e: await self._fail(e, clean_close=False) break # Parse the answer try: answer_type, answer_id, execution_result = self._parse_answer( answer ) except TransportQueryError as e: # Received an exception for a specific query # ==> Add an exception to this query queue # The exception is raised for this specific query, # but the transport is not closed. assert isinstance( e.query_id, int ), "TransportQueryError should have a query_id defined here" try: await self.listeners[e.query_id].set_exception(e) except KeyError: # Do nothing if no one is listening to this query_id pass continue except (TransportServerError, TransportProtocolError) as e: # Received a global exception for this transport # ==> close the transport # The exception will be raised for all current queries. await self._fail(e, clean_close=False) break try: # Put the answer in the queue if answer_id is not None: await self.listeners[answer_id].put( (answer_type, execution_result) ) except KeyError: # Do nothing if no one is listening to this query_id. pass finally: log.debug("Exiting _receive_data_loop()")