A very simple, multi-threaded HTTP server
Project description
Actions Server
A very simple, multi-threaded HTTP server.
Mainly designed for a very simple tasks, f.ex. 3 json-based endpoints with a simple logic
It utilizes a concept of "Actions" - the functions that can be executed to provide HTTP response.
Important note:
This server DOES NOT cover all HTTP functionality. This is intentional and probably will not be changed in the future.
Usage
from actions_server import *
ACTIONS = [
JsonGet("/get", lambda params: {"response": "ok from GET action"}),
JsonPost("/post", lambda params, body: {"response": "ok from POST action"}),
Redirect("/", "/get"),
StaticResources("/static", "./src/web")
]
server = http_server(port=80, actions=ACTIONS, thread_count=5)
try:
server.start(block_caller_thread=True)
finally:
server.stop()
In this example, a server will be started on port 80 and the main thread will be blocked. The server will react on several requests:
curl -X GET "http://localhost:80/get"
will produce{"response": "ok from GET action"}
responsecurl -X POST "http://localhost:8080/post
will produce{"response": "ok from POST action"}
responsecurl -X GET "http://localhost:80/
will send HTTP 301 Redirect to `http://localhost:80/get"curl -X GET "http://localhost:80/static/aaa.png
will return an image./src/web/aaa.png
Available Actions
JsonGet(endpoint, callable)
Will listen to the endpoint endpoint
, call callable(params)
(where params is a dict of arguments; note - values are
always an array!) and convert resulting dict into JSON
JsonPost(endpoint, callable, body)
Will listen to the endpoint endpoint
, call callable(params, body)
(where params is a dict of arguments; note -
values are always an array!, and body is a parsed the request body into dict) and convert resulting dict into JSON
Redirect(from, to)
Will send HTTP 301 Redirect
StaticResources(path, dir)
Will server all files from dir
under path path
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
Hashes for actions_server-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 033c64aae1b3c7622b4279c5bcb50fbdc9e1dbc9ef741d561cc367a574142eaa |
|
MD5 | 1c048fd759d20e13ab4cd39e02dd6864 |
|
BLAKE2b-256 | 8efebd779999d5a85895b50c1b7862ce232d21eb6ccbea9c42d516b0fba7b2f8 |