Python JSON Server & Client
Project description
python-json-socket (jsocket)
Simple JSON-over-TCP sockets for Python. This library provides:
- JsonClient/JsonServer: length‑prefixed JSON message framing over TCP
- ThreadedServer: a single-connection server running in its own thread
- ServerFactory/ServerFactoryThread: a per‑connection worker model for multiple clients
It aims to be small, predictable, and easy to integrate in tests or small services.
Install
pip install jsocket
Requires Python 3.8+.
Quickstart
Echo server with ThreadedServer and a client:
import time
import jsocket
class Echo(jsocket.ThreadedServer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.timeout = 2.0 # sets both accept and recv timeouts
# Return a dict to send a response back to the client
def _process_message(self, obj):
if isinstance(obj, dict) and 'echo' in obj:
return obj
return None
# Bind to an ephemeral port (port=0)
server = Echo(address='127.0.0.1', port=0)
_, port = server.socket.getsockname()
server.start()
client = jsocket.JsonClient(address='127.0.0.1', port=port)
assert client.connect() is True
payload = {"echo": "hello"}
client.send_obj(payload)
assert client.read_obj() == payload
client.close()
server.stop()
server.join()
Per‑connection workers with ServerFactory:
import jsocket
class Worker(jsocket.ServerFactoryThread):
def __init__(self):
super().__init__()
self.timeout = 2.0 # sets recv timeout for this worker
def _process_message(self, obj):
if isinstance(obj, dict) and 'message' in obj:
return {"reply": f"got: {obj['message']}"}
server = jsocket.ServerFactory(Worker, address='127.0.0.1', port=5489)
server.start()
# Connect one or more clients; one Worker is spawned per connection
API Highlights
-
JsonClient:
connect()returns True on successsend_obj(dict)sends a JSON objectread_obj()blocks until a full message is received; raisessocket.timeoutorRuntimeError("socket connection broken")timeoutsets both accept and recv timeoutsaccept_timeoutcontrols the server's accept timeoutrecv_timeoutcontrols the connection read timeout
-
ThreadedServer:
- Subclass and implement
_process_message(self, obj) -> Optional[dict] - Return a dict to send a response; return
Noneto send nothing start(),stop(),join()manage the server threadsend_obj(dict)sends to the currently connected client
- Subclass and implement
-
ServerFactory / ServerFactoryThread:
ServerFactoryThreadis a worker that handles one client connectionServerFactoryaccepts connections and spawns a worker per client
Examples and Tests
- Examples: see
examples/example_servers.pyandscripts/smoke_test.py - Pytest: end-to-end and listener tests under
tests/- Run:
pytest -q
- Run:
Behavior-Driven Tests (Behave)
-
Steps live under
features/steps/and environment hooks infeatures/environment.py. -
To run Behave scenarios, add one or more
.featurefiles underfeatures/and run:pip install -r requirements-dev.txtPYTHONPATH=. behave -f progress2
-
A minimal example feature:
Feature: Echo round-trip Scenario: client/server echo Given I start the server And I connect the client When the client sends the object {"echo": "hi"} Then the client sees a message {"echo": "hi"}
Notes
- Breaking change: version 2.0.0 uses a new framing header (magic + length + CRC32). v1 clients are incompatible.
- Message framing uses a 12‑byte header: 4‑byte magic, 4‑byte big‑endian length, and 4‑byte CRC32 of the payload, followed by a JSON payload encoded as UTF‑8.
max_message_sizedefaults to 10MB; set.max_message_sizeto adjust or set toNoneto disable.- On disconnect, reads raise
RuntimeError("socket connection broken")so callers can distinguish cleanly from timeouts. - Binding with
port=0lets the OS choose an ephemeral port; find it withserver.socket.getsockname().
Links
- PyPI: https://pypi.org/project/jsocket/
- License: see
LICENSE
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 jsocket-2.0.1.tar.gz.
File metadata
- Download URL: jsocket-2.0.1.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee238c1760de0a349f12658eb2fd5ff4c47af4adb9a93ec4048ca39e182eafd8
|
|
| MD5 |
377372d9701fc62588bad2d37fc12075
|
|
| BLAKE2b-256 |
aa118dab5b79a8e4607d6ffec11892369290145270f3280d47b7135b7b9b1ecb
|
File details
Details for the file jsocket-2.0.1-py3-none-any.whl.
File metadata
- Download URL: jsocket-2.0.1-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02cd7e237923e72d99b4aff53a1dc116f826d8db8816b8d83df1cc063f043969
|
|
| MD5 |
192acafd14ab790459de16f68134e064
|
|
| BLAKE2b-256 |
d5dc6ca723053c5ae05ad527d55b3bf6723a33c12bf55bdda9c3c2fa093e85e3
|