A simple tcp server framework inspired by flask.
-
Prepare a json to claim data format.
config_file = "demo_config.json"
-
Code your own business functions and then register them with decorators:
app = TCPApp(config=config_file) @app.function(id=1) def test(*, x): # Do something return {"y": x + 1000}
you can also registere error handlers
@app.error_handler(exc=TypeError, code=1) def handle(e): # Do something return 16, "type error"
-
Start your server:
app.run()
-
Communicate the server with cilent in another process:
from tcp_server.server import TestClient
host = "localhost"
config_file = "demo_config.json"
client = TestClient(host=host, config=config_file)
test_request = {
"x": 2,
"request_no": 1,
"instruction_id": 1,
}
client.test(test_request)