Simple JSON-over-TCP sockets for Python. This library provides:
- JsonClient / JsonServer: encapsulated JSON message framing over TCP
- Blocking Socket IO
- 1:1 Client / Server Connection (no Mult-Client support)
It aims to be small, predictable, and easy to integrate in tests or small services.
pip install jsocket
Requires Python 3.8+.
Echo server with JsonServer:
class JSONServer(jsocket.JsonServer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def _process_message(self, call_obj):
if isinstance(call_obj, dict):
print('Server recv():{}'.format(call_obj))
return call_obj
return { "Status": "Wrong object type received" }
server = JSONServer(
address='127.0.0.1'
).server_loop()Client part:
client_ref = jsocket.JsonClient(
address='127.0.0.1'
)
assert client_ref.connect() is True
client_ref.send_obj({"Echo": "Hello World!"})
res = client_ref.read_obj()
print('Client recv() from server:{}'.format(res))- JsonClient:
connect()returns True on successsend_obj(dict)sends a JSON objectread_obj()blocks until a full message is received
- Examples: see
examples/example_servers.py - Pytest: end-to-end and listener tests under
tests/- Run:
pytest -q
- Run:
- Binding with
port=0lets the OS choose an ephemeral port; find it withserver.socket.getsockname().
- PyPI: https://pypi.org/project/jsocket/
- License: see
LICENSE