How to use JSON-RPC with in Python?

socketio json

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

Install Flask, Flask-SocketIO and eventlet to take requests and jsonrpcserver to process them:

pip install flask flask-socketio eventlet jsonrpcserver

Create a

from flask import Flask
from flask_socketio import SocketIO, send
from jsonrpcserver import method, Result, Success, dispatch

app = Flask(__name__)
socketio = SocketIO(app)

def ping() -> Result:
    return Success("pong")

def handle_message(request):
    if response := dispatch(request):
        send(response, json=True)

if __name__ == "__main__":, port=5000)

Start the server:

$ python
(27985) wsgi starting up on