Skip to main content

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_exact guarantees 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

  1. All messages MUST be framed using MessageProtocol.write_frame.\
  2. Payloads MUST NOT include their own headers (the class adds one automatically).\
  3. Messages longer than _MAX_PAYLOAD (default 4096 bytes) will be rejected.\
  4. Payloads of length 0 are valid (useful for pings/keepalives).

🧪 Example Wire Format

For payload = b"ABC":

0003 41424
  • 0003 → header = 3 bytes\
  • 414243 → payload ("ABC")

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

message_proto-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

message_proto-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

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

Hashes for message_proto-0.1.0.tar.gz
Algorithm Hash digest
SHA256 53367256d210487af0f732e06044e5a51fd7984ed30e40a4d805434c30099e97
MD5 5825fd0e457afc55ceb8c5f6b9cf1ee2
BLAKE2b-256 9df184361ac5221ef7a747688c379985df607d580f29d6c7299216136a75a23f

See more details on using hashes here.

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

Hashes for message_proto-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fc304e926ab695c0f54b3f33e62d856b7f2971cf9daa7a23eea77599e6bceff
MD5 dccf1113566ed2e98f2efeb92048ae61
BLAKE2b-256 743747198b086b51f83a0a266755b956d16ae47739a9ed89983766aa0a77c3c6

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