Skip to main content

easy-sock

A friendly, batteries-included wrapper around Python's built-in socket module - with sensible message framing, timeouts, JSON helpers, file transfer and a threaded server loop.

  • No dependencies - standard library only
  • TCP and UDP
  • Length-prefixed, newline-delimited or raw framing
  • Context-manager friendly
  • Typed, with py.typed

Installation

pip install easy-sock

Quick start

Echo server

from easy_sock import EasySocket


def handle(client, address):
    print("connected:", address)
    for message in client:
        client.send(message)


server = EasySocket.server("0.0.0.0", 9000)
print("listening on", server.address)
try:
    server.serve_forever(handle)
except KeyboardInterrupt:
    server.stop()
finally:
    server.close()

Client

from easy_sock import EasySocket

with EasySocket.connect_to("127.0.0.1", 9000) as sock:
    sock.send("hello server")
    print(sock.recv())

JSON messages

sock.send_message({"type": "ping", "id": 42})
reply = sock.recv_message()

Sending a file

sock.send_file("report.pdf")
path = sock.recv_file(destination="./downloads")

API overview

Member Description
EasySocket(host, port, **opts) Create a socket
EasySocket.server(host, port, **opts) Create a bound, listening TCP server
EasySocket.connect_to(host, port, **opts) Create + connect a client
.bind() / .listen() / .accept() Classic server calls
.connect() Connect the client
.send(data) / .recv() Send/receive one framed message
.send_message(obj) / .recv_message() JSON helpers
.send_file(path) / .recv_file(dir) File transfer
.sendto(data, addr) / .recvfrom() UDP helpers
.serve_forever(handler, max_connections=...) Threaded accept loop
.settimeout(t) / .shutdown() / .close() Lifecycle
.address / .peer / .closed / .socket Introspection

Options

Option Default Meaning
protocol "tcp" "tcp" or "udp"
timeout None Socket timeout in seconds
buffer_size 4096 Bytes read per recv()
encoding "utf-8" Codec for str payloads
framing "length" "length", "newline" or "raw"
delimiter b"\n" Terminator for "newline" framing
max_message_size 64 MiB Safety cap for a single message

Exceptions

SocketEasyError
  +-- ConnectionClosed
  +-- SocketTimeout
  +-- ProtocolError

Development

git clone https://github.com/yourname/easy-sock
cd easy-sock
python -m venv .venv
.venv\Scripts\activate
pip install -e ".[dev]"
pytest

Publishing

python -m build
twine check dist/*
twine upload dist/*

License

MIT (c) 2025 Your Name

Metadata

Release files for easy-socketer 0.1.0

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

Source distribution (sdist)

Source distribution for easy-socketer 0.1.0
File Size Uploaded
easy_socketer-0.1.0.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for easy-socketer 0.1.0
File Interpreter ABI Platform
easy_socketer-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.0 kB

Release files / easy_socketer-0.1.0.tar.gz

Download URL easy_socketer-0.1.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d64227b9617c71f5400483c9a799195a802a0475fdb2849703de41db9160bf63
BLAKE2b-256 checksum
How to use checksums
e62f5eb5aaa4fa351fc8d5019400d24de4599beeb6e394b3dc76c20754a84552
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / easy_socketer-0.1.0-py3-none-any.whl

Download URL easy_socketer-0.1.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8533ab0e359f0372ba061f54cbfcf4f72f222a48d35ce5d8ba6feaf21e126167
BLAKE2b-256 checksum
How to use checksums
1d4fb6aabd66f78d2cdf57192a3577c7f19aa208a35ac4794d2fa993e0fbd4ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

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