Skip to main content

relst

Expose any python object as an API, and call it from python as if it were local. One server, three wire formats: json, xml and a very small binary protocol, plus a swagger-like console to trace and hand-craft requests.

PyPI

Installation

pip install relst

Python 3.10+. Optional desktop client extras: pip install "relst[gui]". Source code: https://github.com/mon231/relst

Quick start

Server:

from relst.server import RelstTextualServer


class Calculator:
    """Server's help string"""

    def add(self, a: int, b: int = 10) -> int:
        "Help string of this method"
        return a + b

    async def shout(self, text: str) -> str:
        return text.upper()

    def _not_exposed(self):
        return 'MySecretCode'


RelstTextualServer(1736, Calculator()).serve()

Client:

from relst.client import RelstFlaskClient

with RelstFlaskClient('127.0.0.1', 1736, protocol='binary') as client:   # json | xml | binary
    print(client.add(1, b=2))      # 3, server methods became python methods
    print(client.shout('hi'))      # 'HI'
    print(client.METHOD_HELP('add'))

Open http://127.0.0.1:1736/_relst/ui for the console. More in examples/.

What gets exposed

Every public (not starting with _) method of the handler object: instance methods, staticmethod, classmethod, and async def coroutines. Arguments are keyword arguments, validated against the method signature before the call (400 on mismatch). Type hints are used to rebuild values: float accepts ints, bytes accepts hex strings, and firstclass / dataclass hints accept dicts.

Return values may be None, bool, int, float, str, bytes, lists/tuples/sets, dicts, enums, dataclasses and firstclass objects.

Failures reach the client as RelstRemoteError(code, message):

code meaning
400 malformed payload, bad / missing arguments, bad value for a firstclass argument
404 unknown (or private) method
413 request bigger than 1 MiB (http)
415 unsupported Content-Type
500 the method raised, the message is generic, details stay in the server log

Built-in methods (__get_class_name, __get_class_help, __get_methods_list, __get_method_help, __update_server) power the client's CLASS_NAME(), CLASS_HELP(), METHOD_HELP(name), UPDATE_SERVER() and UPDATE_METHODS(). Uppercase client names are reserved, a server method can never shadow them.

Servers and clients

class transport protocols
RelstTextualServer(port, handler, host='127.0.0.1', enable_ui=True, trace_limit=500) http (waitress + flask) json, xml, binary by Content-Type
RelstBinaryServer(port, handler, host='127.0.0.1') raw tcp, connections are reused binary
RelstFlaskClient(address, port, is_https=False, protocol='json') http json, xml, binary
RelstSocketClient(address, port) tcp (thread-safe, auto-reconnect) binary

Servers bind to 127.0.0.1 by default; pass host='0.0.0.0' to expose them. serve() blocks, start() / stop() / with server: run in the background (port=0 picks a free port, read it from server.port).

Protocols

The protocol of an http request is picked by Content-Type (default json), the response uses the same protocol unless Accept names another one. The method name is the url path: POST /add.

json (application/json)

Body is an object of keyword arguments, the response body is the returned value. Things json can't carry are wrapped: bytes as {"$bytes": "00ff"}, nan/inf as {"$float": "nan"} (a dict that itself has $-keys is wrapped in {"$dict": ...}). Errors: {"error": {"code": 404, "message": "..."}} with the matching http status.

xml (application/xml)

<request method="add">
  <arg name="a"><value type="int">1</value></arg>
</request>
<!-- response -->  <response><value type="int">11</value></response>
<!-- error -->     <error code="404">Method x does not exist</error>

Value types: none bool int float str bytes list dict; <value> without a type is a string, strings that xml can't carry (control characters, \r) travel as encoding="base64". DTDs / entities are rejected.

binary (application/octet-stream, also raw tcp)

Built for minimal traffic: a 1-byte header, varint sizes (unsigned LEB128, uint32 range), no magic, no padding.

frame    = header:1  [name_size:varint name:utf8]  payload_size:varint  payload
header   = FrameKind:2 bits | version:2 bits | reserved:4 bits       (a firstclass bitfield struct)
FrameKind: REQUEST=0  RESPONSE=1  ERROR=2
  • The name part exists only in requests, and is empty (1 byte) over http where the url has the method.
  • An empty payload is {} in a request and None in a response.
  • An error payload is the value [code, message].
value (first byte)
  1xxxxxxx   small uint 0..127                  (1 byte total)
  01xxxxxx   str, 0..63 utf8 bytes follow
  001xxxxx   list, 0..31 elements follow
  0001xxxx   dict, 0..15 key/value pairs follow
  0000xxxx   ValueTag: NONE=0 FALSE=1 TRUE=2 INT=3 FLOAT32=4 FLOAT64=5 STR=6 BYTES=7 LIST=8 DICT=9
             INT is a zigzag varint (any size), STR/BYTES/LIST/DICT carry a varint size or count,
             floats are 4 bytes when that is lossless, else 8 (big-endian)

The same call {"prefix": "pre", "text": "txt", "count": 3, "flag": true, "blob": b"\x00\x01"}: json 86 bytes, xml 324 bytes, binary 46 bytes (python examples/wire_formats.py).

Safety limits (all protocols): nesting depth 64, 1 MiB per http body / tcp frame, forged lengths and counts are rejected before allocating, bad input is always a 400 / RelstProtocolError, never a crash.

Console (swagger-like UI)

http://<host>:<port>/_relst/ui on every RelstTextualServer:

  • Methods: signatures, docstrings, a generated form per method; run it in json, xml or binary.
  • Generate request: shows the raw request (text, or hex for binary) without sending it. Edit the bytes freely and Send raw to test malformed or hand-written payloads.
  • Traces: live table of everything the server handled (http, tcp and console calls) with raw request/response, status and duration. GET /_relst/traces?after=<id> returns the same as json, DELETE /_relst/traces clears it. RelstBinaryServer.traces is available in code for tcp.

Disable with enable_ui=False. Executing methods from the console needs a json POST (so other websites can't trigger calls through your browser), keep the default 127.0.0.1 bind unless you trust the network.

Development

git clone https://github.com/mon231/relst
cd relst
pip install -e ".[dev]"
python -m pytest tests

The suite (250+ tests) covers property-based round trips of every protocol, fuzzed / mutated / hostile payloads (XML entity bombs, deep nesting, huge ints, forged binary lengths), http and tcp end-to-end behavior, concurrency, server restarts and the console endpoints.

Publishing:

python -m build
python -m twine check dist/*
python -m twine upload dist/*

Metadata

Release files for relst 1.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 relst 1.1.0
File Size Uploaded Provenance Actions
relst-1.1.0.tar.gz 40.0 kB Without provenance Details for relst-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 72.3 kB

Release files / relst-1.1.0.tar.gz

Download URL relst-1.1.0.tar.gz
Size 40.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9905dabca6100c45e63a55e3dce65c3cd0e7585fb55b5361b23a595dfe37bc39
BLAKE2b-256 checksum
How to use checksums
2cb3fc72c070d63fb52a70b8d459089d879f8bbe2a93bbbc872fdaac92284706
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / relst-1.1.0-py3-none-any.whl

Download URL relst-1.1.0-py3-none-any.whl
Size 32.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4d48d7ed838322dff1fb78ef8a163d93a40233176e6266988a90779c0cd349b
BLAKE2b-256 checksum
How to use checksums
d0e580a964df76af690c3a52d0f3962ab1a7ad3ea472899736beb30a287a166a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for relst 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

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