flask plus json

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

Install jsonrpcserver to process the JSON-RPC requests:

$ pip install jsonrpcserver

Create a views.py:

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from jsonrpcserver import method, dispatch

@method
def ping():
    return "pong"

@csrf_exempt
def jsonrpc(request):
    response = dispatch(request.body.decode())
    return JsonResponse(
        response.deserialized(), status=response.http_status, safe=False
    )

Start the development server on port 5000:

$ python manage.py runserver 5000
Performing system checks...

System check identified no issues (0 silenced).
October 29, 2016 - 13:37:51
Django version 1.10.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CONTROL-C.

Client

Use jsonrpcclient to send requests:

$ pip install "jsonrpcclient[requests]"
$ jsonrpc --send http://localhost:5000 ping
'pong'