Neil Booth
8 years ago
5 changed files with 138 additions and 10 deletions
@ -0,0 +1,47 @@ |
|||
#!/usr/bin/env python3 |
|||
|
|||
# See the file "LICENSE" for information about the copyright |
|||
# and warranty status of this software. |
|||
|
|||
import argparse |
|||
import asyncio |
|||
import json |
|||
from os import environ |
|||
|
|||
import aiohttp |
|||
|
|||
|
|||
async def send(url, payload): |
|||
data = json.dumps(payload) |
|||
|
|||
async with aiohttp.post(url, data = data) as resp: |
|||
return await resp.json() |
|||
|
|||
|
|||
def main(): |
|||
'''Send the RPC command to the server and print the result.''' |
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument('--port', metavar='port_num', type=int, |
|||
help='specify the RPC port number') |
|||
parser.add_argument('command', nargs='*', default=[], |
|||
help='send a command to the server') |
|||
args = parser.parse_args() |
|||
|
|||
if args.port is None: |
|||
args.port = int(environ.get('ELECTRUMX_RPC_PORT', 8000)) |
|||
|
|||
url = 'http://127.0.0.1:{:d}/'.format(args.port) |
|||
payload = {'method': args.command[0], 'params': args.command[1:]} |
|||
task = send(url, payload) |
|||
|
|||
loop = asyncio.get_event_loop() |
|||
try: |
|||
result = loop.run_until_complete(task) |
|||
finally: |
|||
loop.close() |
|||
|
|||
print(result) |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
main() |
@ -0,0 +1,60 @@ |
|||
# See the file "LICENSE" for information about the copyright |
|||
# and warranty status of this software. |
|||
|
|||
import logging |
|||
import traceback |
|||
|
|||
from aiohttp import web |
|||
|
|||
|
|||
class ElectrumRPCServer(object): |
|||
'''ElectrumX's RPC server for localhost.''' |
|||
|
|||
def __init__(self, server): |
|||
self.logger = logging.getLogger('RPCServer') |
|||
self.logger.setLevel(logging.INFO) |
|||
self.server = server |
|||
|
|||
async def request_handler(self, request): |
|||
json_request = await request.json() |
|||
try: |
|||
err, result = await self.json_handler(json_request) |
|||
except Exception as e: |
|||
traceback.print_exc() |
|||
err, result = 1, 'caught exception: {}'.format(e) |
|||
|
|||
id_ = request.get('id') |
|||
if err is None: |
|||
response = { |
|||
'id': id_, |
|||
'error': None, |
|||
'result': result, |
|||
} |
|||
else: |
|||
response = { |
|||
'id': id_, |
|||
'error': {'code': err, 'message': result}, |
|||
'result': None, |
|||
} |
|||
|
|||
return web.json_response(response) |
|||
|
|||
async def json_handler(self, request): |
|||
method = request.get('method') |
|||
id_ = request.get('id') |
|||
params = request.get('params', []) |
|||
handler = getattr(self.server, 'handle_rpc_{}'.format(method), None) |
|||
if not handler: |
|||
return 1, 'unknown method "{}"'.format(method) |
|||
else: |
|||
return await handler(params) |
|||
|
|||
def tasks(self, port): |
|||
self.logger.info('listening on port {:d}'.format(port)) |
|||
app = web.Application() |
|||
app.router.add_post('/', self.request_handler) |
|||
host = '0.0.0.0' |
|||
loop = app.loop |
|||
handler = app.make_handler() |
|||
server = loop.create_server(handler, host, port) |
|||
return [server, app.startup()] |
Loading…
Reference in new issue