A simple messaging protocol written on top of TCP using sockets
Project description
Message Protocol
Originially developed by Raj Adhikari
A simple length-prefixed framing protocol for TCP sockets, designed for building echo servers, chat systems, or any application requiring structured message exchange.
🔄 Changelog !
View change log : here
📦 Overview
Message Protocol (MP) wraps raw TCP streams into discrete messages using the following structure:
[ 2-byte header ][ payload ]
- Header: Unsigned 16-bit big-endian integer (
u16, network order).Represents the number of bytes in the payload.Max: 4096 bytes (configurable).\ - Payload: Raw application data (
bytes).
This framing ensures each call to read_frame() returns exactly one
complete message, regardless of how TCP splits or merges packets.
⚙️ Features
- ✅ Works on both client & server with the same class\
- ✅ Protects against partial reads (
recv_exactguarantees n bytes)\ - ✅ Guards against oversized messages (
_MAX_PAYLOAD = 4096)\ - ✅ Clean separation of framing and application payload
🖥️ Usage
NOTE: Requires socket!
Installation
pip install message-proto
Sending a message
from message_protocol import MessageProtocol
msg = b"hello world"
MessageProtocol.write_frame(sock, msg)
Receiving a message
payload = MessageProtocol.read_frame(sock)
if payload is None:
print("Connection closed")
else:
print("Got:", payload.decode())
📑 Protocol Rules
- All messages MUST be framed using
MessageProtocol.write_frame.\ - Payloads MUST NOT include their own headers (the class adds one automatically).\
- Messages longer than
_MAX_PAYLOAD(default 4096 bytes) will be rejected.\ - Payloads of length
0are valid (useful for pings/keepalives).
🧪 Example Wire Format
For payload = b"ABC":
0003 41424
0003→ header = 3 bytes\414243→ payload ("ABC")
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 message_proto-0.1.0.tar.gz.
File metadata
- Download URL: message_proto-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53367256d210487af0f732e06044e5a51fd7984ed30e40a4d805434c30099e97
|
|
| MD5 |
5825fd0e457afc55ceb8c5f6b9cf1ee2
|
|
| BLAKE2b-256 |
9df184361ac5221ef7a747688c379985df607d580f29d6c7299216136a75a23f
|
File details
Details for the file message_proto-0.1.0-py3-none-any.whl.
File metadata
- Download URL: message_proto-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fc304e926ab695c0f54b3f33e62d856b7f2971cf9daa7a23eea77599e6bceff
|
|
| MD5 |
dccf1113566ed2e98f2efeb92048ae61
|
|
| BLAKE2b-256 |
743747198b086b51f83a0a266755b956d16ae47739a9ed89983766aa0a77c3c6
|