Move pretty printing of sessions to client
Tweak logging
This commit is contained in:
parent
501807bf1a
commit
cb7b7dd1fe
@ -13,6 +13,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import pprint
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from os import environ
|
from os import environ
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ class RPCClient(asyncio.Protocol):
|
|||||||
|
|
||||||
def __init__(self, loop):
|
def __init__(self, loop):
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
|
self.method = None
|
||||||
|
|
||||||
def connection_made(self, transport):
|
def connection_made(self, transport):
|
||||||
self.transport = transport
|
self.transport = transport
|
||||||
@ -28,15 +30,28 @@ class RPCClient(asyncio.Protocol):
|
|||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
self.loop.stop()
|
self.loop.stop()
|
||||||
|
|
||||||
def send(self, payload):
|
def send(self, method, params):
|
||||||
|
self.method = method
|
||||||
|
payload = {'method': method, 'params': params}
|
||||||
data = json.dumps(payload) + '\n'
|
data = json.dumps(payload) + '\n'
|
||||||
self.transport.write(data.encode())
|
self.transport.write(data.encode())
|
||||||
|
|
||||||
def data_received(self, data):
|
def data_received(self, data):
|
||||||
payload = json.loads(data.decode())
|
payload = json.loads(data.decode())
|
||||||
self.transport.close()
|
self.transport.close()
|
||||||
print(json.dumps(payload, indent=4, sort_keys=True))
|
result = payload['result']
|
||||||
|
error = payload['error']
|
||||||
|
if error:
|
||||||
|
print("ERROR: {}".format(error))
|
||||||
|
else:
|
||||||
|
if self.method == 'sessions':
|
||||||
|
fmt = '{:<4} {:>23} {:>7} {:>15} {:>7}'
|
||||||
|
print(fmt.format('Type', 'Peer', 'Subs', 'Client', 'Time'))
|
||||||
|
for kind, peer, subs, client, time in result:
|
||||||
|
print(fmt.format(kind, peer, '{:,d}'.format(subs)
|
||||||
|
client, '{:,d}'.format(int(time)))
|
||||||
|
else:
|
||||||
|
pprint.pprint(result, indent=4)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
'''Send the RPC command to the server and print the result.'''
|
'''Send the RPC command to the server and print the result.'''
|
||||||
@ -52,14 +67,12 @@ def main():
|
|||||||
if args.port is None:
|
if args.port is None:
|
||||||
args.port = int(environ.get('ELECTRUMX_RPC_PORT', 8000))
|
args.port = int(environ.get('ELECTRUMX_RPC_PORT', 8000))
|
||||||
|
|
||||||
payload = {'method': args.command[0], 'params': args.param}
|
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
proto_factory = partial(RPCClient, loop)
|
proto_factory = partial(RPCClient, loop)
|
||||||
coro = loop.create_connection(proto_factory, 'localhost', args.port)
|
coro = loop.create_connection(proto_factory, 'localhost', args.port)
|
||||||
try:
|
try:
|
||||||
transport, protocol = loop.run_until_complete(coro)
|
transport, protocol = loop.run_until_complete(coro)
|
||||||
protocol.send(payload)
|
protocol.send(args.command[0], args.param)
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
except OSError:
|
except OSError:
|
||||||
print('error connecting - is ElectrumX catching up or not running?')
|
print('error connecting - is ElectrumX catching up or not running?')
|
||||||
|
|||||||
@ -31,7 +31,7 @@ class Daemon(util.LoggedClass):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.url = url
|
self.url = url
|
||||||
self._height = None
|
self._height = None
|
||||||
self.logger.info('connecting to daemon at URL {}'.format(url))
|
self.logger.info('connecting at URL {}'.format(url))
|
||||||
self.debug_caught_up = 'caught_up' in debug
|
self.debug_caught_up = 'caught_up' in debug
|
||||||
# Limit concurrent RPC calls to this number.
|
# Limit concurrent RPC calls to this number.
|
||||||
# See DEFAULT_HTTP_WORKQUEUE in bitcoind, which is typically 16
|
# See DEFAULT_HTTP_WORKQUEUE in bitcoind, which is typically 16
|
||||||
|
|||||||
@ -632,15 +632,9 @@ class LocalRPC(JSONRPC):
|
|||||||
|
|
||||||
async def sessions(self, params):
|
async def sessions(self, params):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
fmt = '{:<4} {:>21} {:>7} {:>12} {:>7}'
|
return [(session.kind, session.peername, len(session.hash168s),
|
||||||
result = []
|
session.client, now - session.start)
|
||||||
result.append(fmt.format('Type', 'Peer', 'Subs', 'Client', 'Time'))
|
for session in self.SESSION_MGR.sessions]
|
||||||
for session in self.SESSION_MGR.sessions:
|
|
||||||
result.append(fmt.format(session.kind, session.peername,
|
|
||||||
'{:,d}'.format(len(session.hash168s)),
|
|
||||||
session.client,
|
|
||||||
'{:,d}'.format(int(now - session.start))))
|
|
||||||
return result
|
|
||||||
|
|
||||||
async def numsessions(self, params):
|
async def numsessions(self, params):
|
||||||
return len(self.SESSION_MGR.sessions)
|
return len(self.SESSION_MGR.sessions)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user