Skip to main content

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

Reason this release was yanked:

Se corrigión un bug

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.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.0.tar.gz (47.1 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.0-py3-none-any.whl (59.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastsocket-2.1.0.tar.gz
  • Upload date:
  • Size: 47.1 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.0.tar.gz
Algorithm Hash digest
SHA256 45f8d04e06a1748afaab30ac770fc34ac56e01562e6ac2d718b6926b9dc4ffed
MD5 c5417b9b54b3240c5fb9999edd8d6646
BLAKE2b-256 562aa255eacbd8552d707332baea29871b7c4f6b3bef0d67e29909d2a2a22c72

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastsocket-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 59.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 69a5e8062682f922bbfd94da99c2b676c8bfc892cb3a86f0def14d30793d7608
MD5 223ba061c85f085d3be992686799d110
BLAKE2b-256 c226b652609bacb4f2fae9ba096d34a9953e489f1d94a1b6e42e103f354b63d0

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