This post uses Jsonrpcserver 5 which requires Python 3.8+.

zeromq json

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

Install pyzmq to take requests and jsonrpcserver to process them:

pip install pyzmq jsonrpcserver

Create a server.py:

from jsonrpcserver import method, Result, Success, dispatch
import zmq

socket = zmq.Context().socket(zmq.REP)


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


if __name__ == "__main__":
    socket.bind("tcp://*:5000")
    while True:
        request = socket.recv().decode()
        socket.send_string(dispatch(request))

Start the server:

$ python ./server.py

Client

Use jsonrpcclient to send requests:

pip install pyzmq jsonrpcclient
>>> from jsonrpcclient import request_json, parse_json, Ok
>>> import zmq
>>> socket = zmq.Context().socket(zmq.REQ)
>>> socket.connect("tcp://localhost:5000")
>>> socket.send_string(request_json("ping"))
>>> response = parse_json(socket.recv().decode())
>>> response.result
'pong'