Observed since 20 Oct, 18:34 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
A very simple, multi-threaded HTTP 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.
This server DOES NOT cover all HTTP functionality. This is intentional and probably will not be changed in the future.
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.pngJsonGet(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
from actions_server import *
class MyCustomResponse(Response):
def __init__(self, payload):
self._payload = payload
def headers(self) -> dict:
return {'Content-type': 'text/plain'}
def response_body_bytes(self):
return self._payload.encode('utf-8')
class MyCustomAction(Action):
def can_handle(self, method, path, params, payload):
return method == 'GET' and path == '/hello'
def handle(self, method, path, params, payload) -> Response:
return MyCustomResponse("hello there!")
Notes:
method may contain two strings - GET or POSTDownload counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.0.10 | 23 Jan 2024 | 3 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 18:34 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:dea02af225a265a5c3b5a2e600e4bb927c922bb39c77e1de55a2bc346f86e6df