jsonrpc.py (713B)
1 # standard imports 2 import os 3 import sys 4 5 # local imports 6 from chainlib.jsonrpc import JSONRPCRequest 7 from chainlib.eth.connection import EthHTTPConnection 8 9 # set up node connection and execute rpc call 10 rpc_provider = os.environ.get('RPC_PROVIDER', 'http://localhost:8545') 11 conn = EthHTTPConnection(rpc_provider) 12 13 # check the connection 14 if not conn.check(): 15 sys.stderr.write('node {} not usable\n'.format(rpc_provider)) 16 sys.exit(1) 17 18 # build and send rpc call 19 g = JSONRPCRequest() 20 o = g.template() 21 o['method'] = 'eth_blockNumber' 22 r = conn.do(o) 23 24 # interpret result for humans 25 try: 26 block_number = int(r, 10) 27 except ValueError: 28 block_number = int(r, 16) 29 30 print('block number {}'.format(block_number))