Skip to main content

Librería Python para servidores y clientes TCP/UDP con cifrado híbrido, chunks y transferencia de archivos.

Project description

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 renamedpip 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.
  • PerformanceFileTransfer 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastsocket-2.1.1.tar.gz (45.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastsocket-2.1.1-py3-none-any.whl (60.0 kB view details)

Uploaded Python 3

File details

Details for the file fastsocket-2.1.1.tar.gz.

File metadata

  • Download URL: fastsocket-2.1.1.tar.gz
  • Upload date:
  • Size: 45.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for fastsocket-2.1.1.tar.gz
Algorithm Hash digest
SHA256 a32fdd950ec2cf55810237db9978495317278ad6ba3f066fe7921fc7f4fa0635
MD5 3c5bf15bfc97263fc2e6ff242d68e5cc
BLAKE2b-256 fc12f5c140b707da82b53224453cc6be61d91d0b89d1f5ba6eb4d10f0ef99dd0

See more details on using hashes here.

File details

Details for the file fastsocket-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: fastsocket-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for fastsocket-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e12dda1c62cfac51e89a326a719b109d5bb528476f2b814c5efb3fe41419bc4a
MD5 6195e5ea5456947d60c36356a8d0e322
BLAKE2b-256 4bd5e99b8e65c32559fa07bfd2d4e2709449bb71aac4dcfbed578a6fc631f590

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page