Skip to main content

rpc over websocket uses protobuf to serialize data

Project description

rpc over websocket uses protobuf to serialize data

Prepare

install package

pip install websocket-rpcs

write proto file (math.proto)

syntax = "proto3";

option py_generic_services = true;

message SumArg {
    int32 a = 1;
    int32 b = 2;
}

message SumResult {
    int32 sum = 1;
}

service Math {
    rpc Sum(SumArg) returns (SumResult);
}

generate python file (math_pb2.py math_pb2.pyi)

protoc --python_out=. --pyi_out=. math.proto

Example

Server

import wrpc
import math_pb2
def sum(args):
    return {
        "sum": args["a"] + args["b"]
    }
wrpc_server = wrpc.WebSocketRPCServer("0.0.0.0", 5887)
wrpc_server.add_handler("Math.Sum", sum_pb2.SumArg,sum_pb2.SumResult, sum)
wrpc_server.start()

Client

import wrpc
import math_pb2
wrpc_client = wrpc.WebSocketRpcClient("ws://localhost:5887")
sum_client = wrpc_client.init(sum_pb2.Sum_Stub, sum_pb2.SumArg, sum_pb2.SumResult)
res = sum_client.call("Sum", a=1, b=2)

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

websocket_rpcs-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

websocket_rpcs-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file websocket_rpcs-0.1.0.tar.gz.

File metadata

  • Download URL: websocket_rpcs-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.8 Windows/10

File hashes

Hashes for websocket_rpcs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d126823912fd4c9408f18402b55ea5d7ab4c17f783ca6db6b5f71035148ced0d
MD5 236729a91833d72902d3f8aabdc320b1
BLAKE2b-256 88d8fa5f7b32586b77b635ebb7de4c1bbb3460396610e14ced37f0acabec235c

See more details on using hashes here.

File details

Details for the file websocket_rpcs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: websocket_rpcs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.8 Windows/10

File hashes

Hashes for websocket_rpcs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 748d5a2e3d292c2181bbcb4614115cc872938d88dc7378330a917952d023ce80
MD5 fc572e6047aa907fff3e3ad59f853c83
BLAKE2b-256 4de88c25106a5801cbbffa0c5db2176ca47e6a818d8d5b4753e31adb2015eba7

See more details on using hashes here.

Supported by

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