TCP networking for Python, without the boilerplate.
Project description
SocketFlow
A high-performance, dependency-free TCP networking library for Python with advanced features like compression, event handling, bidirectional keepalive, and more.
Features
- Zero Dependencies - Uses only Python's standard library
- Bidirectional Keepalive - Both client and server independently monitor connection health
- TCP-Level Keepalive - OS-managed keepalive for reliable connection detection
- Compression - Support for zlib, lzma, and bz2 compression
- Event-Driven Architecture - Flexible event dispatcher for handling server/client events
- Blueprint System - Organize your code with reusable blueprints
- Middleware Support - Add custom middleware to request/response processing
- Path-Based Routing - Route messages to specific handlers using paths
- Efficient Buffer Handling - O(N) buffer processing with offset pattern
- Type Hints - Full type annotations for better IDE support
- Cross-Platform - Works on Windows, Linux, and macOS
Installation
pip install socketflow
Full documentation available at: https://socketflow.dev/
Or install from source:
git clone https://github.com/ayammaximilian/socketflow.git
cd socketflow
pip install .
Quick Start
Server Example
from socketflow import TcpServer, EventType
# Create server
server = TcpServer(
host="127.0.0.1",
port=8080,
keepalive_interval=30.0,
keepalive_max_missed=3,
compress=True
)
# Register event handler
@server.event(EventType.Server.MESSAGE)
def handle_message(data):
print(f"Received: {data}")
return "Response"
# Start server
server.start()
server.wait() # Keep server running
Client Example
from socketflow import TcpClient, EventType
# Create client
client = TcpClient(
host="127.0.0.1",
port=8080,
keepalive_interval=30.0,
keepalive_max_missed=3,
compress=True
)
# Connect to server
client.connect()
# Register event handler
@client.event(EventType.Client.MESSAGE)
def handle_message(data):
print(f"Received: {data}")
# Send message
response = client.send("Hello, Server!", wait_response=True)
print(f"Server response: {response}")
# Disconnect
client.disconnect()
Configuration
Server Options
| Parameter | Type | Default | Description |
|---|---|---|---|
host |
str | "127.0.0.1" | Server host address |
port |
int | 8080 | Server port |
compression_type |
str | "zlib" | Compression algorithm (zlib, lzma, bz2) |
compression_level |
int | 6 | Compression level (1-9) |
compress |
bool | True | Enable compression |
keepalive_interval |
float | 30.0 | Keepalive interval in seconds |
keepalive_max_missed |
int | 3 | Max missed keepalives before disconnect |
recv_buffer_size |
int | 65536 | Receive buffer size |
send_buffer_size |
int | 65536 | Send buffer size |
Client Options
| Parameter | Type | Default | Description |
|---|---|---|---|
host |
str | "127.0.0.1" | Server host address |
port |
int | 8080 | Server port |
compression_type |
str | "zlib" | Compression algorithm (zlib, lzma, bz2) |
compression_level |
int | 6 | Compression level (1-9) |
compress |
bool | True | Enable compression |
keepalive_interval |
float | 30.0 | Keepalive interval in seconds |
keepalive_max_missed |
int | 3 | Max missed keepalives before disconnect |
connection_timeout |
float | 10.0 | Connection timeout in seconds |
recv_buffer_size |
int | 65536 | Receive buffer size |
send_buffer_size |
int | 65536 | Send buffer size |
API Reference
TcpServer
Methods
start()- Start the serverstop()- Stop the server and disconnect all clientswait()- Block until server stopsstart_and_wait()- Start server and blocksend_client(client_addr, data, path=None, wait_response=False)- Send data to specific clientdisconnect_client(client_addr)- Disconnect a specific clientget_connected_clients()- Get number of connected clientsis_connected(client_addr)- Check if client is connectedevent(event_type)- Decorator to register event handlerpath(path, middleware=None)- Decorator to register path handlerregister_blueprint(blueprint)- Register a blueprint
TcpClient
Methods
connect()- Connect to serverdisconnect()- Disconnect from serversend(data, path=None, wait_response=False)- Send data to serverwait()- Block until client disconnectsconnect_and_wait()- Connect and blockis_connected()- Check if connectedevent(event_type)- Decorator to register event handlerpath(path, middleware=None)- Decorator to register path handlerregister_blueprint(blueprint)- Register a blueprint
Events
Server Events
EventType.Server.START- Server startedEventType.Server.STOP- Server stoppedEventType.Server.CLIENT_CONNECT- Client connectedEventType.Server.CLIENT_DISCONNECT- Client disconnectedEventType.Server.MESSAGE- Message received from client
Client Events
EventType.Client.CONNECT- Connected to serverEventType.Client.DISCONNECT- Disconnected from serverEventType.Client.MESSAGE- Message received from server
Global Events
EventType.Global.ERROR- Error occurred
Path-Based Routing
Send messages to specific handlers using paths:
# Server
@server.path("/user/login")
def handle_login(data):
# Handle login
pass
@server.path("/user/register")
def handle_register(data):
# Handle registration
pass
# Client
client.send(data, path="/user/login")
Blueprints
Organize your code with blueprints:
from socketflow import Blueprint
user_bp = Blueprint("user")
@user_bp.path("/login")
def login(data):
pass
@user_bp.path("/register")
def register(data):
pass
# Register blueprint
server.register_blueprint(user_bp)
Keepalive
SocketFlow implements bidirectional keepalive at two levels:
- Application-Level Keepalive - Custom ping/pong messages
- TCP-Level Keepalive - OS-managed keepalive probes
Both client and server independently monitor connection health based on their own configurations.
Compression
Support for multiple compression algorithms:
- zlib - Fast compression, good balance
- lzma - High compression ratio, slower
- bz2 - Good compression, moderate speed
Error Handling
SocketFlow provides custom exception types:
NotConnected- Connection not establishedConnectionTimeout- Connection attempt timed outKeepaliveTimeout- Keepalive timeoutCompressionError- Compression/decompression errorInvalidData- Invalid message formatNoResponse- No response received within timeoutMessageHandlerError- Message handling error
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
- GitHub Issues: https://github.com/ayammaximilian/socketflow/issues
- Documentation: https://socketflow.dev/
Requirements
- Python 3.7+
- No external dependencies (uses only standard library)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 socketflow-0.1.1.tar.gz.
File metadata
- Download URL: socketflow-0.1.1.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6b0f3156eaa240b724e56f066b8311c62b358e6fde3d3d7119384171b72d4fa
|
|
| MD5 |
c33d2ad6da63abb2dab191bdb04d44ab
|
|
| BLAKE2b-256 |
17089fa6aeabf129c8c6585df6bbff91cbd6de98cccdb362178bddc31009f6a7
|
File details
Details for the file socketflow-0.1.1-py3-none-any.whl.
File metadata
- Download URL: socketflow-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc965a99a60fe2c5eb122d8ff4c43c054e13bcb4048fd356959238b851f6af4b
|
|
| MD5 |
eea7b104f64a8f2fbc38ac8ba50cd3f1
|
|
| BLAKE2b-256 |
e31114df7919c2a74e15ec1f37845acb6c444bb52100ec03c6434d3f1681a9fd
|