From d33dd4c9a241f3bb70afdbe4eae3814b2affcfb2 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Fri, 10 Nov 2023 13:49:21 +0100 Subject: [PATCH] script: add rpc script to query lilith_spawns() output --- script/lilith_spawns.py | 100 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 script/lilith_spawns.py diff --git a/script/lilith_spawns.py b/script/lilith_spawns.py new file mode 100644 index 000000000..be5613d7a --- /dev/null +++ b/script/lilith_spawns.py @@ -0,0 +1,100 @@ +# This file is part of DarkFi (https://dark.fi) +# +# Copyright (C) 2020-2023 Dyne.org foundation +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +import asyncio, json, random, sys, time + + +class JsonRpc: + + async def start(self, server, port): + reader, writer = await asyncio.open_connection(server, port) + self.reader = reader + self.writer = writer + + async def stop(self): + self.writer.close() + await self.writer.wait_closed() + + async def _make_request(self, method, params): + ident = random.randint(0, 2**16) + #print(ident) + request = { + "jsonrpc": "2.0", + "method": method, + "params": params, + "id": ident, + } + + message = json.dumps(request) + "\n" + self.writer.write(message.encode()) + await self.writer.drain() + + data = await self.reader.readline() + message = data.decode().strip() + response = json.loads(message) + #print(response) + return response + + async def _subscribe(self, method, params): + ident = random.randint(0, 2**16) + request = { + "jsonrpc": "2.0", + "method": method, + "params": params, + "id": ident, + } + + message = json.dumps(request) + "\n" + self.writer.write(message.encode()) + await self.writer.drain() + #print("Subscribed") + + async def ping(self): + return await self._make_request("ping", []) + + async def spawns(self): + return await self._make_request("spawns", []) + +async def main(argv): + rpc = JsonRpc() + while True: + try: + await rpc.start("localhost", 18927) + break + except OSError: + pass + response = await rpc._make_request("spawns", []) + info = response["result"] + spawns = info["spawns"] + + for spawn in spawns: + urls = spawn["urls"] + name = spawn["name"] + hosts = spawn["hosts"] + + print(f"\nname: {name}") + print(f"urls:") + for url in urls: + print(f" {url}") + if hosts: + print(f"hosts:") + for host in hosts: + print(f" {host}") + + await rpc.stop() + + +asyncio.run(main(sys.argv))