Skip to main content

FastSocket

FastSocket

A fast, minimal Python library for building TCP and UDP servers and clients with optional encryption, chunking, and file transfer.

PyPI version Python License: AGPL-3.0 GitHub issues GitHub stars

Documentation · Quick Start · Examples · API Reference · Changelog


Overview

FastSocket is a Python library designed for rapid development and production use. It supports multi-connection handling out of the box and offers three security modes — plain, RSA, and a hybrid TLS-like mode (RSA key exchange + AES-256-GCM + HMAC) — without the complexity of managing raw sockets or TLS certificates.

Features

Feature Description
TCP & UDP Simple API for servers and clients over TCP or UDP
Multi-connection Thread-based multi-client support with callback handlers
RSA encryption Secure mode with RSA-4096 key exchange
Hybrid TLS mode RSA-4096 handshake + AES-256-GCM messaging + HMAC authentication
Auto-reconnect Clients reconnect automatically on connection loss
Disconnect callbacks on_disconnect hook for connection loss events
Graceful shutdown stop() method on all servers and clients
ChunkManager Split and reassemble large payloads reliably
FileTransfer Send and receive files with SHA-256 integrity verification
UDP broadcast Unicast and broadcast UDP support
Examples & benchmarks Ready-to-run examples, benchmarks, and stress tests included

Installation

pip install fastsocket

Requirements: Python 3.8+, pycryptodome (installed automatically).

To install from source:

git clone https://github.com/giulicrenna/FastSocket.git
cd FastSocket
pip install -e .

Quick Start

TCP Server

from fastsocket import FastSocketServer, SocketConfig, Queue

def handle_messages(messages: Queue):
    while not messages.empty():
        msg, addr = messages.get()
        print(f"[{addr}] {msg}")
        server.send_msg_stream(f"Echo: {msg}")

config = SocketConfig(host="localhost", port=8080)
server = FastSocketServer(config)
server.on_new_message(handle_messages)
server.start()

# Graceful shutdown
# server.stop()

TCP Client

from fastsocket import FastSocketClient, SocketConfig

def on_message(msg: str):
    print("Server:", msg)

# Auto-reconnect and disconnect callback (new in 2.1.0)
client = FastSocketClient(
    SocketConfig(host="localhost", port=8080),
    auto_reconnect=True,
    reconnect_delay=2.0,
)
client.on_new_message(on_message)
client.on_disconnect(lambda: print("Connection lost, reconnecting…"))
client.start()
client.send_to_server("Hello FastSocket")

# Graceful shutdown
# client.stop()

Hybrid TLS Mode (RSA + AES-256-GCM + HMAC)

from fastsocket import TLSSocketServer, TLSSocketClient, SocketConfig

# Server
server = TLSSocketServer(SocketConfig(host="localhost", port=9443), shared_secret="strong-secret")
server.on_new_message(lambda q: print(q.get()))
server.start()

# Client (auto_reconnect supported)
client = TLSSocketClient(
    SocketConfig(host="localhost", port=9443),
    shared_secret="strong-secret",
    auto_reconnect=True,
)
client.on_new_message(lambda msg: print(msg))
client.on_disconnect(lambda: print("TLS connection lost"))
client.start()

UDP

from fastsocket import FastSocketUDPServer, SocketConfig
import socket

config = SocketConfig(host="0.0.0.0", port=9000, type=socket.SOCK_DGRAM)
server = FastSocketUDPServer(config, enable_broadcast=True)
server.start()

Available Classes

Servers

Class Protocol Encryption
FastSocketServer TCP None
SecureFastSocketServer TCP RSA-4096
TLSSocketServer TCP RSA + AES-256-GCM + HMAC
FastSocketUDPServer UDP None

All servers expose stop() for graceful shutdown.

Clients

Class Protocol Encryption
FastSocketClient TCP None
SecureFastSocketClient TCP RSA-4096
TLSSocketClient TCP RSA + AES-256-GCM + HMAC
FastSocketUDPClient UDP None

FastSocketClient and TLSSocketClient support on_disconnect and auto_reconnect. All clients expose stop() / close().

Utilities

Class Description
ChunkManager Split and reassemble large byte payloads
FileTransfer File transfer with progress callbacks and hash verification
SocketConfig Configuration object for host, port, and socket type

What's new in 2.1.1

  • Package renamed — pip install fastsocket (was FastSocket). Import also updated: from fastsocket import ....

What's new in 2.1.0

  • Busy-loop fix — message handler threads now sleep when queues are idle instead of spinning at 100% CPU.
  • stop() API — every server and client can now be shut down cleanly.
  • on_disconnect + auto-reconnect — TCP clients notify user code on disconnect and can reconnect automatically (including full TLS handshake re-negotiation).
  • Race condition fix — UDP server dict access is now protected by a lock.
  • RSA size guard — oversized messages raise BadEncryptionInput instead of a cryptic pycryptodome error.
  • Performance — FileTransfer uses framing for metadata (no more byte-by-byte reads); ChunkManager uses bytearray to avoid O(n²) copies.

See the full Changelog for details.

Documentation

Full documentation is available at giulicrenna.github.io/FastSocket, including:

Roadmap

  • Native TLS/SSL support (via Python ssl stdlib)
  • async/await API (asyncio)
  • Message compression (zlib / lz4)
  • Exportable metrics (Prometheus / JSON)
  • WebSocket support
  • Channel multiplexing over a single connection
  • Bindings for other languages (Go, Rust)

See the full Roadmap for details.

Contributing

Pull requests are welcome. Before submitting, make sure tests pass and code follows PEP 8.

git clone https://github.com/giulicrenna/FastSocket.git
cd FastSocket
pip install -e ".[dev]"
pytest tests/

See Contributing Guide for full instructions.

Contact

Author: Giuliano Crenna
Email: giulicrenna@gmail.com
GitHub: github.com/giulicrenna

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).
See the LICENSE file for details.

Metadata

Release files for fastsocket 2.1.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 fastsocket 2.1.1
File Size Uploaded
fastsocket-2.1.1.tar.gz 45.6 kB Details

Built distribution (wheel)

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

Total release size: 105.6 kB

Release files / fastsocket-2.1.1.tar.gz

Download URL fastsocket-2.1.1.tar.gz
Size 45.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a32fdd950ec2cf55810237db9978495317278ad6ba3f066fe7921fc7f4fa0635
BLAKE2b-256 checksum
How to use checksums
fc12f5c140b707da82b53224453cc6be61d91d0b89d1f5ba6eb4d10f0ef99dd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Release files / fastsocket-2.1.1-py3-none-any.whl

Download URL fastsocket-2.1.1-py3-none-any.whl
Size 60.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e12dda1c62cfac51e89a326a719b109d5bb528476f2b814c5efb3fe41419bc4a
BLAKE2b-256 checksum
How to use checksums
4bd5e99b8e65c32559fa07bfd2d4e2709449bb71aac4dcfbed578a6fc631f590
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1.0.0

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