Skip to main content

RPCUDP : RPC over UDP in Python

Build Status

RPC over UDP may seem like a silly idea, but things like the DHT Kademlia require it. This project is specifically designed for asynchronous Python 3 code to accept and send remote proceedure calls.

Because of the use of UDP, you will not always know whether or not a procedure call was successfully received. This isn't considered an exception state in the library, though you will know if a response isn't received by the server in a configurable amount of time.

Installation

pip install rpcudp

Usage

This assumes you have a working familiarity with asyncio.

First, let's make a server that accepts a remote procedure call and spin it up.

import asyncio
from rpcudp.protocol import RPCProtocol

class RPCServer(RPCProtocol):
    # Any methods starting with "rpc_" are available to clients.
    def rpc_sayhi(self, sender, name):
        return "Hello %s you live at %s:%i" % (name, sender[0], sender[1])

    # You can also define a coroutine
    async def rpc_sayhi_slowly(self, sender, name):
        await some_awaitable()
        return "Hello %s you live at %s:%i" % (name, sender[0], sender[1])

async def main():
    loop = asyncio.get_event_loop()
    # start a server on UDP port 1234
    transport, protocol = await loop.create_datagram_endpoint(
        RPCServer, local_addr=("127.0.0.1", 1234)
    )

    # run indefinitely
    await asyncio.Event().wait()

asyncio.run(main())

Now, let's make a client script. Note that we do need to specify a port for the client as well, since it needs to listen for responses to RPC calls on a UDP port.

import asyncio
from rpcudp.protocol import RPCProtocol

async def sayhi(protocol, address):
    # result will be a tuple - first arg is a boolean indicating whether a response
    # was received, and the second argument is the response if one was received.
    result = await protocol.sayhi(address, "Snake Plissken")
    print(result[1] if result[0] else "No response received.")

async def main():
    loop = asyncio.get_event_loop()
    # Start local UDP server to be able to handle responses
    transport, protocol = await loop.create_datagram_endpoint(
        RPCProtocol, local_addr=("127.0.0.1", 4567)
    )

    # Call remote UDP server to say hi
    await sayhi(protocol, ("127.0.0.1", 1234))
    transport.close()

asyncio.run(main())

You can run this example in the examples folder (client.py and server.py).

Logging

This library uses the standard Python logging library. To see debut output printed to STDOUT, for instance, use:

import logging

log = logging.getLogger('rpcudp')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())

Implementation Details

The protocol is designed to be as small and fast as possible. Python objects are serialized using MsgPack. All calls must fit within 8K (generally small enough to fit in one datagram packet).

Metadata

Release files for rpcudp 5.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rpcudp 5.0.1
File Size Uploaded
rpcudp-5.0.1.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rpcudp 5.0.1
File Interpreter ABI Platform
rpcudp-5.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / rpcudp-5.0.1.tar.gz

Download URL rpcudp-5.0.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b6793b9b3e84e9c8510fa78e259cc3204c1b03fd2bb4fbf0f457cd391933bb78
BLAKE2b-256 checksum
How to use checksums
395b99ee4dd6080d857f029ad209860d461305f5fba9fef2316548a1d131e4c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release files / rpcudp-5.0.1-py3-none-any.whl

Download URL rpcudp-5.0.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8bf7cf1caed687acbbf2a37b67a92b02d109483a2da28fa13cca93b9ee873b9a
BLAKE2b-256 checksum
How to use checksums
058b33f35f2a730f848b5e5ac24dcb50c97387b98756e0f199ee8da30ef19fe7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release history Release notifications | RSS feed

This release

5.0.1 This release

2 release files

5.0.0

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.1

1 release file

2.0

1 release file

1.0

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

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