Skip to main content

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)

Release files for websocket-rpcs 0.1.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 websocket-rpcs 0.1.0
File Size Uploaded
websocket_rpcs-0.1.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for websocket-rpcs 0.1.0
File Interpreter ABI Platform
websocket_rpcs-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / websocket_rpcs-0.1.0.tar.gz

Download URL websocket_rpcs-0.1.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d126823912fd4c9408f18402b55ea5d7ab4c17f783ca6db6b5f71035148ced0d
BLAKE2b-256 checksum
How to use checksums
88d8fa5f7b32586b77b635ebb7de4c1bbb3460396610e14ced37f0acabec235c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.8 Windows/10

Release files / websocket_rpcs-0.1.0-py3-none-any.whl

Download URL websocket_rpcs-0.1.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
748d5a2e3d292c2181bbcb4614115cc872938d88dc7378330a917952d023ce80
BLAKE2b-256 checksum
How to use checksums
4de88c25106a5801cbbffa0c5db2176ca47e6a818d8d5b4753e31adb2015eba7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.8 Windows/10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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