Skip to main content

aiohttp_jrpc

https://github.com/zloidemon/aiohttp_jrpc/actions/workflows/build.yml/badge.svg https://badge.fury.io/py/aiohttp_jrpc.svg

jsonrpc protocol implementation for aiohttp.web.

Example server

import asyncio
from aiohttp import web
from aiohttp_jrpc import Service, JError, jrpc_errorhandler_middleware

SCH = {
    "type": "object",
    "properties": {
        "data": {"type": "string"},
    },
}

async def custom_errorhandler_middleware(app, handler):
    async def middleware(request):
        try:
            return (await handler(request))
        except Exception:
            """ Custom errors: -32000 to -32099 """
            return JError().custom(-32000, "Example error")
    return middleware

class MyJRPC(Service):
    @Service.valid(SCH)
    async def hello(self, ctx, data):
        if data["data"] == "hello":
            return {"status": "hi"}
        return {"status": data}

    async def error(self, ctx, data):
        raise Exception("Error which will catch middleware")

    async def no_valid(self, ctx, data):
        """ Method without validation incommig data """
        return {"status": "ok"}

async def init(loop):
    app = web.Application(loop=loop, middlewares=[jrpc_errorhandler_middleware])
    #app = web.Application(loop=loop, middlewares=[custom_errorhandler_middleware])
    app.router.add_route('POST', "/api", MyJRPC)

    srv = await loop.create_server(app.make_handler(),
                                        "127.0.0.1", 8080)
    print("Server started at http://127.0.0.1:8080")
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

Example client

import asyncio
import aiohttp
from aiohttp_jrpc import Client, InvalidResponse

URL = 'http://localhost:8080/api'

async def rpc_call():
    try:
        async with aiohttp.ClientSession() as client:
            Remote = Client(client, URL)
            rsp = await Remote.call('hello', {'data': 'hello'})
            return rsp
    except InvalidResponse as err:
        return err
    except Exception as err:
        return err
    return False

loop = asyncio.get_event_loop()
content = loop.run_until_complete(rpc_call())
print(content.result)
loop.close()

License

aiohttp_jrpc BSD license.

CHANGES

0.2.0 (2026-07-16)

  • Added new tests

  • async functions in classes after removing coroutine decorator

  • Use enum with error messages

  • Updated to work on latest python and aiohttp

0.1.0 (2016-02-20)

  • Added client and tests

  • Changed BSD v3 to BSD v2 license

0.0.3 (2015-10-27)

  • Fix messages of protocol errors

  • Fix tests and add tests for custom errors

  • Fix example bugs

  • Added custom middleware to example for handle errors

0.0.2 (2015-10-22)

  • Added middleware to catch exceptions

  • Testing internal error

0.0.1 (2015-10-18)

  • Init release

Credits

aiohttp_jrpc is written by Veniamin Gvozdikov.

Contributors

Please add yourself here alphabetically when you submit your first pull request.

Metadata

Release files for aiohttp-jrpc 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-jrpc 0.2.0
File Size Uploaded Provenance Actions
aiohttp_jrpc-0.2.0.tar.gz 8.2 kB Without provenance Details for aiohttp_jrpc-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-jrpc 0.2.0
File Interpreter ABI Platform Actions
aiohttp_jrpc-0.2.0-py3-none-any.whl Python 3 none any Details for aiohttp_jrpc-0.2.0-py3-none-any.whl

Total release size: 15.9 kB

Release files / aiohttp_jrpc-0.2.0.tar.gz

Download URL aiohttp_jrpc-0.2.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c864f77630e77180aebfc031dc289f28cca908a91416fc73a60359a8eb7d6404
BLAKE2b-256 checksum
How to use checksums
4e81b1900059087d4e23df2a132f00353f02bd8a102774cdc6e4e779ea7b694a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / aiohttp_jrpc-0.2.0-py3-none-any.whl

Download URL aiohttp_jrpc-0.2.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92f2011971d70884649ddd0978cddbfac6fcda52394f7db31a8e8583b77b676d
BLAKE2b-256 checksum
How to use checksums
f363234e5e72ec17d68200adcbd75d9e09e2e1ad5c2d5df74aee7167786607d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for aiohttp-jrpc 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page