def ws_connect(self): cnt = 0 while True: cnt += 1 self.url = next(self.nodes) logger.debug("Trying to connect to node %s" % self.url) if self.url[:3] == "wss": sslopt_ca_certs = {'cert_reqs': ssl.CERT_NONE} self.ws = websocket.WebSocket(sslopt=sslopt_ca_certs) else: self.ws = websocket.WebSocket() try: self.ws.connect(self.url) break except KeyboardInterrupt: raise except: if self.num_retries >= 0 and cnt > self.num_retries: raise NumRetriesReached() sleeptime = (cnt - 1) * 2 if cnt < 10 else 10 if sleeptime: logger.warning( "Lost connection to node during wsconnect(): %s (%d/%d) " % (self.url, cnt, self.num_retries) + "Retrying in %d seconds" % sleeptime) time.sleep(sleeptime)
def call(self, name, *args, api=None, return_with_args=None, _ret_cnt=0): body = WsClient.json_rpc_body(name, *args, api=api) response = None cnt = 0 while True: cnt += 1 try: logger.debug(body) self.ws.send(body) response = self.ws.recv() logger.debug(response) break except KeyboardInterrupt: raise except: if self.num_retries > -1 and cnt > self.num_retries: raise NumRetriesReached() sleeptime = (cnt - 1) * 2 if cnt < 10 else 10 if sleeptime: logger.warning( "Lost connection to node during call(): %s (%d/%d) " % (self.url, cnt, self.num_retries) + "Retrying in %d seconds" % sleeptime) time.sleep(sleeptime) # retry try: self.ws.close() time.sleep(sleeptime) self.ws_connect() except: pass return self._return(response=response, args=args, return_with_args=return_with_args)