betterlib

A useful collection of various tools and libraries for anyone to use in their projects.


Project maintained by HENRYMARTIN5 Hosted on GitHub Pages — Theme by mattgraham

Quik

This module contains the QuikServer class, which is a simple and easy to set up HTTP server that can be used to serve files or dynamically generated content.

QuikServer

The QuikServer class is a simple HTTP server that can be used to serve files or dynamically generated content. It allows for simple set-up of the complex systems set in place by Python’s http.server in all but a few lines of code.

Constructor

The constructor for the QuikServer class takes the following arguments:

Methods

The QuikServer class has the following methods:

Handlers

Handlers are functions that are called when a request is received by the server. They are triggered on specific paths and can be set to recieve only certain request methods. They return a QuikResponse object, which takes the default parameters of the response content and the status code. Handlers are added to the server using the add_handler() method.

For instance:

def handler():
    return QuikResponse("Hello, world!", 200)
server.add_handler("/", handler) # Assuming server is an instance of QuikServer

This will add a handler that will be called when a request is made to the root path (/) and will return a response with the content “Hello, world!” and a status code of 200.

Handlers can also be set to only be called on certain request methods. For instance:

def handler():
    return QuikResponse("Hello, world!", 200)
server.add_handler("/", handler, methods=["GET"]) # Once again, assuming server is an instance of QuikServer

This will only be called if the request method is GET. If the request method is not GET, the handler will not be called and the server will return a 405 error.

In order to recieve a request body, the handler must take a parameter called body. If one is not specified and the request method is POST, the server will return a 500 error.

Example:

def handler(body=None):
    return QuikResponse(body, 200)
server.add_handler("/", handler, methods=["POST"])

QuikResponse

The QuikResponse class is a simple class that represents a response to a request. It takes the following arguments: