tornado plus json

We’ll use Tornado to take JSON-RPC requests. It should respond to “ping” with “pong”.

Install Tornado to take requests and jsonrpcserver to process them:

$ pip install tornado jsonrpcserver

Create a

from tornado import ioloop, web
from jsonrpcserver.aio import methods
from jsonrpcserver.response import NotificationResponse

async def ping():
    return 'pong'

class MainHandler(web.RequestHandler):
    async def post(self):
        request = self.request.body.decode()
        response = await methods.dispatch(request)
        if not isinstance(response, NotificationResponse):

app = web.Application([(r"/", MainHandler)])

if __name__ == '__main__':

Start the server:

$ python

Synchronous client

Use jsonrpcclient to send requests.

$ pip install 'jsonrpcclient[requests]'
$ python
>>> from jsonrpcclient.http_client import HTTPClient
>>> HTTPClient('http://localhost:5000/').request('ping')
--> {"jsonrpc": "2.0", "method": "ping", "id": 1}
<-- {"jsonrpc": "2.0", "result": "pong", "id": 1} (200 OK)

Asynchronous client with Tornado

We can send asynchronous requests in Tornado with jsonrpcclient (thanks to saaj):

$ pip install 'jsonrpcclient[tornado]'

Create a

from tornado import ioloop
from jsonrpcclient.tornado_client import TornadoClient

client = TornadoClient('http://localhost:5000/')

async def main():
    result = await client.request('ping')


The async/await syntax requires Python 3.5+. Prior to that use @gen.coroutine and yield.

$ python
INFO:jsonrpcclient.client.request:{"jsonrpc": "2.0", "method": "ping", "id": 1}
INFO:jsonrpcclient.client.response:{"jsonrpc": "2.0", "result": "pong", "id": 1}