def query(self, method, params): params = [params] if type(params) is not list else params msg = json.dumps({ "id": 0, "method": method, "params": params }, sort_keys=True) qid = self._query_id_str(msg) if ".onion" in self.server: print("Electrum fetch: %s" % chill_green_str(qid)) else: print("Electrum fetch: %s" % chill_yellow_str(qid)) if self.cache and self.dm.is_query_cached(qid): cache_file = self.dm.get_query_cache_path(qid) return FileManager.read_json_info(cache_file) self._create_socket() self.socket.send(msg.encode() + b'\n') msg = self._recv_line() response = json.loads(msg.decode()) self._destroy_socket() if 'error' in response.keys(): sys.exit("*** Error from Electrum server: %s" % json.dumps(response, sort_keys=True)) if self.cache: cache_file = self.dm.get_query_cache_path(qid) FileManager.write_text(cache_file, json.dumps(response)) return response
def _create_socket(self): s = (socks.create_connection( (self.server, self.port), proxy_type=socks.SOCKS5, proxy_addr="127.0.0.1", proxy_port=9050) if self.tails else socket.create_connection( (self.server, self.port))) self.socket = ssl.wrap_socket(s) if self.ssl else s print("opened connection to: %s" % chill_yellow_str("%s:%d" % (self.server, self.port)))
def _text_dl(self, url): if ".onion" in url: print("tor fetch: %s" % chill_green_str(url)) else: print("web fetch: %s" % chill_yellow_str(url)) dm = DirectoryManager() if self.cache and dm.is_query_cached(url): cache_file = dm.get_query_cache_path(url) return FileManager.read_text(cache_file) t = self._request(url) if self.cache: cache_file = dm.get_query_cache_path(url) FileManager.write_text(cache_file, t) return t
def query(self, method, params): params = [params] if type(params) is not list else params msg = json.dumps({ "id": 0, "method": method, "params": params }, sort_keys=True) qid = self._query_id_str(msg) print("Electrum fetch: %s" % chill_yellow_str(qid)) if self.cache and self.dm.is_query_cached(qid): cache_file = self.dm.get_query_cache_path(qid) return FileManager.read_json_info(cache_file) self._create_socket() self.socket.send(msg.encode() + b'\n') response = json.loads(self.socket.recv(RECV_MAX)[:-1].decode()) self._destroy_socket() if self.cache: cache_file = self.dm.get_query_cache_path(qid) FileManager.write_text(cache_file, json.dumps(response)) return response
def _destroy_socket(self): self.socket.close() print("closed connection to: %s" % chill_yellow_str("%s:%d" % (self.server, self.port)))