Working on my cluster-backups I wanted an RPC-like mechanism for communication from client -> server, but I required decoupled operation of the clients and server. I wanted to "magically" serialize data of arbitrary length, simplify the logic (abstracting out checking for error conditions etc), but if possible maintain long-lived TCP connections between client and server.
Above all else, code using datagrams should be very natural, readable, and very easy to use.
A simple "echo" server:
from datagram import * s = DatagramServer("localhost", 5005) while True: with s.accept() as datagram: message = datagram.value().upper() datagram.send(message.upper())
The client:
from datagram import * buffer = "a" * 1000 with Datagram(buffer, server="localhost", port=5005) as datagram: if datagram.send(): # or send(server="localhost", port=5000) print(datagram.receive())
The datagramwill evaluate True or False based on the most recent connection. if datagram is great. datagram.value() returns its deserialized contents (which is either what was most recently sent or received). len() and contains/in treats it like a list, and it can return iterables like for token in datagram or if token in datagram