1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00
electrs/contrib/client.py

23 lines
548 B
Python
Raw Normal View History

2018-04-30 13:37:23 +02:00
import json
import socket
2018-04-25 21:44:36 +02:00
2019-08-03 13:46:39 +02:00
class Client:
2018-05-02 12:42:13 +02:00
def __init__(self, addr):
self.s = socket.create_connection(addr)
self.f = self.s.makefile('r')
self.id = 0
def request(self, method, *args):
self.id += 1
return {
2018-05-02 12:42:13 +02:00
'id': self.id,
'method': method,
'params': list(args),
2021-03-26 09:05:58 +01:00
'jsonrpc': '2.0',
2018-05-02 12:42:13 +02:00
}
def call(self, *requests):
msg = json.dumps(requests) + '\n'
2018-05-02 12:42:13 +02:00
self.s.sendall(msg.encode('ascii'))
return json.loads(self.f.readline())