Basic binary socket based rpc for Python
Project description
basic-socket-rpc
It's in the name, basic RPC implementation with just enough functionality to solve my own problem.
Currently implemented a threaded server and a blocking client though there's no reason an async server/client couldn't be added.
See greet_server.py and greet_client.py for example usage.
Constraints:
- Supports up to 16k registered commands (due to low level command id represented as a 16 bit integer)
- Runs on Python 3.6+.
Pros:
- No dependencies
- Support for managing amount of concurrent users of the RPC service. (I need exclusive access).
- Client/Server decoupled from user code.
- Some basic support for exceptions.
Cons:
- No encryption/authentication
- user code responsible for serializing/deserializing messages.
The RPC protocol is not aware of the type of arguments/responses being sent.
I.e. the message payload is tightly coupled with user code.
The tediousness of this is mitigated somewhat by using composable
serializer/deserializer functions such as
make_serializer. See conftest.py for an example of this. The serialization helper functions reside in rpc_serialization_functions. - No autogeneration logic to generate RPC APIs.
- parsing of large messages slow due to inefficient copying
Explanation of how it works
This RPC library provides provides (de-)serialization functions to serialize data primitives to bytes which are then transmitted over a socket using a minimal protocol. Basically just prefixing the serialized data with a message type and a size. See rpc_low_level.py for more info. You are expected to construct the serializer and deserializer for the function you wish to use over an RPC channel yourself and if they don't match, then you'll get a message transmission error that is hard to debug. The simplest thing to do is to visually compare your definition of the server and client and see if the (de-)serialization names match.
For example, say the server definition is: (from greet_server.py)
greet_server_spec = RpcServerSpec(
responses=(
RpcServerResp(
cmd_id=greet_server_cmd_ids.hello,
parse_and_call=make_server_deserializer(deserialize_str),
serialize_response=serialize_str,
client_function=lambda name: f"hello {name}",
),
RpcServerResp(
cmd_id=greet_server_cmd_ids.goodbye,
parse_and_call=make_server_deserializer(deserialize_str),
serialize_response=serialize_str,
client_function=lambda name: f"goodbye {name}",
),
RpcServerResp(
cmd_id=greet_server_cmd_ids.add_2_words,
parse_and_call=make_server_deserializer(parse_int_from_le_bytes_4, parse_int_from_le_bytes_4),
serialize_response=int_to_le_bytes_4,
client_function=lambda a, b: a + b,
),
RpcServerResp(
cmd_id=greet_server_cmd_ids.func_no_args,
parse_and_call=call_no_args,
serialize_response=serialize_no_response,
client_function=lambda: print("hello with no args") or None,
),
),
)
With the associated client definition:
greet_client_spec = RpcClientSpec(
requests=[
RpcClientReq(
cmd_id=greet_client_cmd_ids.hello,
serialize_request=serialize_str,
parse_response=deserialize_str_only,
),
RpcClientReq(
cmd_id=greet_client_cmd_ids.goodbye,
serialize_request=serialize_str,
parse_response=deserialize_str_only,
),
RpcClientReq(
cmd_id=greet_client_cmd_ids.add_2_words,
serialize_request=make_serializer(int_to_le_bytes_4, int_to_le_bytes_4),
parse_response=int_from_le_bytes_4,
),
RpcClientReq(
cmd_id=greet_client_cmd_ids.func_no_args,
serialize_request=serialize_no_response,
parse_response=parse_no_response,
),
],
)
Note how the (de-)serialization functions names are somewhat reciprocal.
The add_2_words command on the server side deserializes its two arguments with
make_server_deserializer(parse_int_from_le_bytes_4, parse_int_from_le_bytes_4),.
On the client side those two arguments are serialized with
serialize_request=make_serializer(int_to_le_bytes_4, int_to_le_bytes_4),
Similarly with the hello command. The server side deserializes the command
argument with make_server_deserializer(deserialize_str) and client serializes
it with serialize_request=serialize_str.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file basic_socket_rpc-0.6.1-py3-none-any.whl.
File metadata
- Download URL: basic_socket_rpc-0.6.1-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f53b4529037ba027b4021c0ec92047e4d2a40a3ff02152ada7fe43f024d8a5c9
|
|
| MD5 |
fb56bc141e5d8140b6cac9ac7a363216
|
|
| BLAKE2b-256 |
903d4eefc80fa7bd89abb2741f9fdf280e1b41d5849381166d7b6158956d7d3d
|