Git Product home page Git Product logo

async4jsonrpc's Introduction

GitHub | Gitee

async4jsonrpc

This library is an asynchronous python library for the JSON-RPC specification.

It is licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html).

Installation

Install from PyPi via pip:

pip3 install async4jsonrpc

Features

  • Compliant with the JSON-RPC 2.0 specification
  • High performance by asyncio and coroutine
  • Json serialization support via jsonpickle

Usage

Server Usage

server-test.py

from async4jsonrpc.server import JsonRpcServer

# create RPC json server
server = JsonRpcServer()
# registers a function to respond to RPC requests.
server.register_function(lambda x, y: x + y, 'add')
server.register_function(lambda x: x, 'ping')
# start to serve RPC request
server.serve('localhost', 8080)

Client Usage

client-test.py

import asyncio
from async4jsonrpc.client import JsonRpcClient

# create RPC json client
client = JsonRpcClient('127.0.0.1', 8080)

async def call_rpc(i):
    # rpc: send rpc request
    r = await client.ping(f'hello {i}')
    print(r)

asyncio.run(call_rpc('shi'))

Json serialize

I use jsonpickle library for serialization and deserialization of complex Python objects to and from JSON

  1. Python object class example: thing.py
class Thing(object):
    def __init__(self, name):
        self.name = name
  1. Client call rpc with a python object: client-test.py
import asyncio
from async4jsonrpc.client import JsonRpcClient
from tests.thing import Thing

# create RPC json client
client = JsonRpcClient('127.0.0.1', 8080)

async def call_object_rpc():
    obj = Thing('Awesome')
    r = await client.ping(obj)
    print(r)

asyncio.run(call_object_rpc())

TODO

  1. improve

async4jsonrpc's People

Contributors

shigebeyond avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.