Last active
May 21, 2024 23:39
-
-
Save st3b1t/6861207d2c1cb29da05b5f9289b8c423 to your computer and use it in GitHub Desktop.
client electrs RPC for commandline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
import importlib.util | |
import argparse | |
import json | |
## | |
# usage examples: | |
# ./electrs-client.py server.version | |
# ./electrs-client.py mempool.get_fee_histogram | |
## edit with your cutom electrs directory | |
ELECTRS_PATH="/home/electrs" | |
## | |
def module_from_path(path, name): | |
spec = importlib.util.spec_from_file_location(name, path) | |
module = importlib.util.module_from_spec(spec) | |
spec.loader.exec_module(module) | |
return module | |
client = module_from_path(ELECTRS_PATH+'/contrib/client.py', 'client') | |
def main(method, params): | |
conn = client.Client(('localhost', 50001)) | |
resp = conn.call([client.request(method, params)]) | |
print(json.dumps(resp)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
# Aggiungi il primo argomento (obbligatorio) | |
parser.add_argument("method", type=str, help="RPC Method") | |
# Aggiungi un argomento che cattura tutti gli argomenti successivi al primo | |
parser.add_argument("params", nargs='*', help="Parameters") | |
args = parser.parse_args() | |
print("method:", args.method) | |
print("params:", args.params) | |
main(args.method, args.params) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment