route
This module contains all the utilities to launch a micro server from python lib. It is not recommended to use it in production mode.
$ python route.py -h
Usage: route.py [options] BINDINGS...
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-t THREADS, --threads=THREADS
set thread number [default: 2]
-l LOGLEVEL, --log-level=LOGLEVEL
set log level from 1 to 100 [default: 20]
-i HOST, --ip=HOST ip to run from [default: 127.0.0.1]
-p PORT, --port=PORT port to use [default: 5000]
BINDINGS
is a list of python modules containing python bound functions.
Endpoint Objects
class Endpoint()
Placeholder for storing endpoint mappings.
EndpointAlreadyDefined Objects
class EndpointAlreadyDefined(Exception)
Exception raised when an endpoint is already defined.
UrlMatchError Objects
class UrlMatchError(Exception)
Exception raised for errors in URL pattern matching.
uHTTPRequestHandler Objects
class uHTTPRequestHandler(BaseHTTPRequestHandler)
Custom HTTP request handler that handles HTTP methods dynamically.
Methods:
format_response
- Formats the response as JSON.do_
- Processes HTTP requests based on registered endpoints.
uHTTPRequestHandler.__getattr__
def __getattr__(attr: str) -> Callable
Dynamically handles HTTP methods like ‘do_GET’, ‘do_POST’, etc.
Arguments:
attr
str - The attribute name.
Returns:
Callable
- The dynamic handler function.
uHTTPRequestHandler.format_response
@staticmethod
def format_response(resp: tuple) -> typing.Tuple[str, str]
Formats a response as JSON.
Arguments:
resp
Any - The response data.
Returns:
Tuple[str, str]: The JSON response and its content type.
uHTTPRequestHandler.do_
def do_(method: str = "GET") -> int
Processes an HTTP request and calls the appropriate endpoint.
Arguments:
method
str - The HTTP method (e.g., “GET”, “POST”). Defaults to “GET”.
Returns:
int
- Status code of the response.
bind
def bind(path: str,
methods: list = ["GET"],
target: BaseHTTPRequestHandler = uHTTPRequestHandler) -> Callable
Binds a function to a specific URL path and HTTP methods.
Arguments:
path
str - The URL path to bind.methods
list - List of HTTP methods (e.g., [“GET”, “POST”]).target
BaseHTTPRequestHandler - The request handler class.
Returns:
Callable
- A decorator that binds the function.
callback
def callback(url: str, headers: dict, data: str, function: Callable,
method: str, markups: OrderedDict, regexp: re.Pattern,
arg_spec: inspect.FullArgSpec) -> typing.Any
Handles the execution of the bound function with appropriate arguments. the last 4 parameters are defined by the bind function and stored in the lambda callback.
Arguments:
url
str - The full URL of the request.headers
dict - The HTTP headers from the request.data
str - The body of the request.function
Callable - The function to execute.method
str - The HTTP command used.markups
OrderedDict - Mappings of path variables to their types.regexp
re.Pattern - Compiled regex for path matching.arg_spec
FixArgSpec - Argument specification of the function.
Returns:
Any
- The result of the function execution.
run
def run(host: str = "127.0.0.1", port: int = 5000, loglevel: int = 20) -> None
Starts the HTTP server.
Arguments:
host
str - The IP address to bind to. Defaults to “127.0.0.1”.port
int - The port to bind to. Defaults to 5000.loglevel
int - Logging level. Defaults to 20.