Skip to main content

FastTcpAPI

FastAPI-style command routing for TCP services with customizable frames.

Install

pip install fasttcpapi

Server

import asyncio
from fasttcpapi import Server

server = Server()

@server.command("echo")
def echo(message: str, prefix: str = ""):
    return {"message": prefix + message}

@server.command("async_echo")
async def async_echo(message: str):
    return {"message": message}

@server.command("count", response_frames=2)
def count():
    yield 1
    yield 2

@server.command("async_count", response_frames=2)
async def async_count():
    yield {"tick": 1}
    yield {"tick": 2}

if __name__ == "__main__":
    server.exec("127.0.0.1", 9000)

Both def and async def handlers may use return for one response. Both regular generators and async generators may use yield for multiple responses; set response_frames to the exact number of yielded values. Use timeout to control the client wait time. Any exception raised by a handler is converted by the frame implementation and returned to the client.

Pushes

import time

@server.push("clock.push")
async def clock_push():
    while True:
        yield {"unix_time": time.time()}
        await asyncio.sleep(1)

The push handler starts once for each connected client.

Client

import asyncio
from fasttcpapi import Client

async def main():
    client = Client("127.0.0.1", 9000)
    print(await client.echo("FastTcpAPI", prefix="Hello, "))
    push = await client.next_push()
    print(push.command, push.args)
    await client.close()

asyncio.run(main())

The command proxy uses __call__ and validates arguments from the server definition before sending a request. Multi-frame commands return a list. A missing response raises TimeoutError; remote errors are raised by Frame.result().

For blocking applications:

client = Client("127.0.0.1", 9000, sync=True)
value = client.echo("FastTcpAPI")

For a concurrent.futures.Future:

future = client.submit("echo", "FastTcpAPI")
value = future.result()

One Client instance can be used from multiple threads and event loops. Call await client.close() when it is no longer needed.

Custom Frames

Implement Frame and pass the class to Server. The framework does not impose a wire layout. Your frame defines how to encode and decode the command, arguments, keyword arguments, session ID, and protocol-specific metadata.

from fasttcpapi import Frame, Server

class MyFrame(Frame):
    async def decode_from_reader(self, reader): ...
    def parse_args(self, param_list): ...
    def decode_from_result(self, result, request): ...
    def decode_from_exception(self, exception, request): ...
    def result(self): ...
    def encode(self) -> bytes: ...

server = Server(MyFrame)

decode_from_result and decode_from_exception receive the original request frame, so responses can reuse its session ID and custom metadata.

For binary payloads, decode_typed_arguments supports int, float, bool, NUL-terminated str, and ctypes types:

from fasttcpapi import decode_typed_arguments
values = decode_typed_arguments(payload, param_list, byteorder="little")

See examples/ for complete JSON and binary frame implementations.

Callbacks

Both Server and Client support optional synchronous or asynchronous logging callbacks:

server.add_on_request_callback(on_request)
server.add_on_response_callback(on_response)
server.add_on_push_callback(on_push)

client.add_on_request_callback(on_request)
client.add_on_response_callback(on_response)
client.add_on_push_callback(on_push)

FastTcpAPI remains an alias for Server.

Release files for fasttcpapi 0.1.2

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

Source distribution (sdist)

Source distribution for fasttcpapi 0.1.2
File Size Uploaded
fasttcpapi-0.1.2.tar.gz 19.1 kB Details

Built distribution (wheel)

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

Total release size: 36.5 kB

Release files / fasttcpapi-0.1.2.tar.gz

Download URL fasttcpapi-0.1.2.tar.gz
Size 19.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8a82cdd66b7ccb81ee9562486ac01ae8bf77185c271a27a7a19793b23d6e2910
BLAKE2b-256 checksum
How to use checksums
3f9f52b9bc0f76ab91060a27ea757dbc966546f111bad2eba12009ddd81efeec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / fasttcpapi-0.1.2-py3-none-any.whl

Download URL fasttcpapi-0.1.2-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d31cef681d4c157e0806964d278da5fb5799d7e70e3d7cc3b6d45904d6a094ec
BLAKE2b-256 checksum
How to use checksums
a6de2d1204ecb2c58322bb5577391270b316163d74d5799a981378c9823d7366
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

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