Skip to main content

Transport agnostic framework for developing OpenRPC servers.

Project description

Python OpenRPC

Documentation: https://python-openrpc.burkard.cloud

Source Code: https://gitlab.com/mburkard/openrpc

Python OpenRPC is a transport agnostic framework for quickly and easily developing OpenRPC servers in Python.

Requirements

Installation

OpenRPC is on PyPI and can be installed with:

pip install openrpc

Or with Poetry

poetry add openrpc

Example

This is a minimal OpenRPC server using a Sanic websocket server as the transport method.

import asyncio

from openrpc import RPCServer
from sanic import Request, Sanic, Websocket

app = Sanic("DemoServer")
rpc = RPCServer(title="DemoServer", version="1.0.0")


@rpc.method()
async def add(a: int, b: int) -> int:
    return a + b


@app.websocket("/api/v1/")
async def ws_process_rpc(_request: Request, ws: Websocket) -> None:
    async def _process_rpc(request: str) -> None:
        json_rpc_response = await rpc.process_request_async(request)
        if json_rpc_response is not None:
            await ws.send(json_rpc_response)

    async for msg in ws:
        asyncio.create_task(_process_rpc(msg))


if __name__ == "__main__":
    app.run()

Example In

{
  "id": 1,
  "method": "add",
  "params": {
    "a": 1,
    "b": 3
  },
  "jsonrpc": "2.0"
}

Example Result Out

{
  "id": 1,
  "result": 4,
  "jsonrpc": "2.0"
}

Template App

You can bootstrap your OpenRPC server by cloning the template app.

Support the Developer

Buy Me a Coffee

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openrpc-7.3.1.tar.gz (16.1 kB view hashes)

Uploaded Source

Built Distribution

openrpc-7.3.1-py3-none-any.whl (20.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page