Skip to main content

python rpc based on udp

Project description

udprpc Package

This is a rpc package based on udp. Its usage is very simple.

Now the package can only be used under python3, because it is implemented by asyncio.

Besides, you can also use it on the bash shell.

1. Install

pip install udp_rpc

2. API Example


2.1 server:

from udprpc.server import BaseRPCServer

class MyRpcServer(BaseRPCServer):

    def rpc_add(self, x, y):
        print("{x} + {y} = {z}".format(
            x=x, y=y, z=x + y
        ))

if __name__ == "__main__":
    rpc_server = MyRpcServer("192.168.1.102", 8080, max_threads=3)
    rpc_server.start()

All methods with the prefix rpc_ can be called by the client.


2.2 async client:

import asyncio
from udprpc.client import AsyncClient

loop = asyncio.get_event_loop()

loop.run_until_complete(AsyncClient.dial("192.168.1.102", 8080))

AsyncClient.call("add", 1, 2)

2.3 normal client:

from udprpc.client import Client

client = Client("192.168.1.102", 8080)

client.call("add", 1, 2)

client.close()

##3. Command Example funcs.py

import redis

db = redis.StrictRedis(host="10.0.86.24",db=5)

def rpc_set_data(k, v):
    db.set(k, v)
    print("set:", k, v)

def rpc_get_data(k):
    print(db.get(k))

def rpc_del_data(k):
    db.delete(k)
    print("del:", k)

启动:

udpserver funcs.py -h 127.0.0.1 -p 9898

客户端调用:

import asyncio
from udprpc.client import AsyncClient

loop = asyncio.get_event_loop()

loop.run_until_complete(AsyncClient.dial("127.0.0.1", 9898))
AsyncClient.call("set_data", "a", 1)
AsyncClient.call("get_data", "a")

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
udp_rpc-0.1.0-py3-none-any.whl (4.6 kB) Copy SHA256 hash SHA256 Wheel py3
udp_rpc-0.1.0.tar.gz (3.7 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page