Skip to main content

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"} response
  • curl -X POST "http://localhost:8080/post will produce {"response": "ok from POST action"} response
  • curl -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

Actions out-of-the-box

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

Implementing custom action

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:

  • parameter method may contain two strings - GET or POST
  • response body must be bytes!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

actions-server-0.0.8.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

actions_server-0.0.8-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file actions-server-0.0.8.tar.gz.

File metadata

  • Download URL: actions-server-0.0.8.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for actions-server-0.0.8.tar.gz
Algorithm Hash digest
SHA256 852988ae25065f8be5e8101e34a007620aad720e8f06640843024b1954604a47
MD5 f285f827dc0a0c2b3dce711eb717004b
BLAKE2b-256 b97e379d574319db93aa692b92b3bb92a95330fb5e46382d8697ccbdb5b627bb

See more details on using hashes here.

File details

Details for the file actions_server-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for actions_server-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 0cc55b4633650032b6db84b5c7ed90c6934279515f2f076bbe02aa787d3d0bb3
MD5 43dd6321f51b2cae14c1130d31b7c20f
BLAKE2b-256 f9b49333c044249336fcea5d691068b34b49daa9c705c9bddeaf1ababf9a1a47

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page