Skip to main content

Send and receive JSONs over byte streams - sockets, pipes, files, and more - using a universal, length-prefixed binary framing scheme.

Project description

send-recv-json

Send and receive JSONs over byte streams - sockets, pipes, files, and more - using a universal, length-prefixed binary framing scheme.

Usage

  • For send_json, provide a send: Callable[[bytes], int] function that accepts bytes and returns number of bytes sent.
    • If we cannot send anymore (i.e., byte stream is closed), throws IOError.
  • For recv_json, provide a recv: Callable[[int], bytes] function that accepts a byte count and returns bytes with length <= that byte count.
    • If we cannot recv anymore (i.e., byte stream is closed), throws EOFError.

Over TCP Sockets

# coding=utf-8
from __future__ import print_function
import socket
from send_recv_json import send_json, recv_json

# Server
def server():
    sock = socket.socket()
    sock.bind(('localhost', 9999))
    sock.listen(1)
    conn, addr = sock.accept()
    # Receive a JSON object from the client
    obj = recv_json(conn.recv)
    print(obj)
    # Send a response
    send_json(conn.send, {"status": "ok"})
    conn.close()

# Client
def client():
    sock = socket.socket()
    sock.connect(('localhost', 9999))
    # Send a JSON object
    send_json(sock.send, {"msg": "hello"})
    # Receive a response
    resp = recv_json(sock.recv)
    print(resp)
    sock.close()

# Run server() in one process, client() in another.

Between Processes (pipes, os.read/os.write)

# coding=utf-8
from __future__ import print_function
import os
from send_recv_json import send_json, recv_json

read_file_descriptor, write_file_descriptor = os.pipe()

# In child process:
send_json(lambda b: os.write(write_file_descriptor, b), {"pid": 123})

# In parent process:
data = recv_json(lambda n: os.read(read_file_descriptor, n))
print(data)  # Output: {'pid': 123}

Logging to and Reading from Files

# coding=utf-8
from __future__ import print_function
from os import write
from os.path import getsize
from send_recv_json import send_json, recv_json

with open('events.log', 'wb') as f:
    # In Python 2, the built-in file object's `.write()` method returns `None` (both in text and binary mode).
    # In Python 3, it returns the number of chars/bytes written.
    # For compatibility, we use os.write instead
    send_json(lambda data: write(f.fileno(), data), {"event": "start", "user": "alice"})
    send_json(lambda data: write(f.fileno(), data), {"event": "stop", "user": "bob"})

with open('events.log', 'rb') as f:
    while True:
        try:
            print(recv_json(f.read))
        except EOFError:
            break

Installation

pip install send-recv-json

Use Case Examples

  • Passing objects between local processes over pipes
  • High-throughput network messaging (microservices, custom servers)
  • Durable event logging in binary files
  • Embedded systems communication
  • Specialized "glue" between Python and other languages/tools
  • Cross-language communication (cloud-native, rapid scripts, etc.)

Philosophy

Unlike “kitchen sink” frameworks, send-recv-json does just one thing - robust, high-performance JSON serialization/deserialization and framing over byte streams.

  • No heavy abstractions, magic, or dependencies.
  • Drop in as a primitive under your protocol, event system, or microservice stack.

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

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

send_recv_json-0.1.0a1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

send_recv_json-0.1.0a1-py2.py3-none-any.whl (4.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file send_recv_json-0.1.0a1.tar.gz.

File metadata

  • Download URL: send_recv_json-0.1.0a1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for send_recv_json-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 2fd3b5318c1ac685941b209a307d03ab89a1d7c9edac936ce23b05cdc35e114d
MD5 05af687bc6d05f638c0870183ac947dd
BLAKE2b-256 909bf7aa09780ae48e5f93d8dc418d72639c68fdcb8e8e5965c8ae3a1d199aee

See more details on using hashes here.

File details

Details for the file send_recv_json-0.1.0a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for send_recv_json-0.1.0a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 477f3dae1a6570a3d4d0a7a53d1de646109d6a77b53244625acdbe83fd2b1308
MD5 c048cd8518661f15abb86485d6467513
BLAKE2b-256 40bc9c0429a9213dec3ac6d28e655902df9a97ebf67fdd0af982162e724b9b10

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