Pre-release
This release is a pre-release and may not be stable for production use.
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 asend: Callable[[bytes], int]function that acceptsbytesand returns number of bytes sent.- If we cannot send anymore (i.e., byte stream is closed), throws
IOError.
- If we cannot send anymore (i.e., byte stream is closed), throws
- For
recv_json, provide arecv: Callable[[int], bytes]function that accepts a byte count and returnsbyteswith length<=that byte count.- If we cannot recv anymore (i.e., byte stream is closed), throws
EOFError.
- If we cannot recv anymore (i.e., byte stream is closed), throws
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.
Release files for send-recv-json 0.1.0a1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| send_recv_json-0.1.0a1.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| send_recv_json-0.1.0a1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 8.7 kB
Release files / send_recv_json-0.1.0a1.tar.gz
| Download URL | send_recv_json-0.1.0a1.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2fd3b5318c1ac685941b209a307d03ab89a1d7c9edac936ce23b05cdc35e114d
|
|
BLAKE2b-256 checksum How to use checksums |
909bf7aa09780ae48e5f93d8dc418d72639c68fdcb8e8e5965c8ae3a1d199aee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / send_recv_json-0.1.0a1-py2.py3-none-any.whl
| Download URL | send_recv_json-0.1.0a1-py2.py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
477f3dae1a6570a3d4d0a7a53d1de646109d6a77b53244625acdbe83fd2b1308
|
|
BLAKE2b-256 checksum How to use checksums |
40bc9c0429a9213dec3ac6d28e655902df9a97ebf67fdd0af982162e724b9b10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|