blite
- minimal web backend framework
Usage
- first step is to import the library and instantiate a
Serverobject, passing the host and port to bind to the server
from blite import Server, renderFile
HOST = '0.0.0.0'
PORT = 8080
server = Server(HOST, PORT)
-
logging level can be set to:
- silent executing
server.setLogLevel(LogLevel.SILENT) - concise executing
server.setLogLevel(LogLevel.CONCISE) - verbose executing
server.setLogLevel(LogLevel.VERBOSE)
- silent executing
-
then define the routes, using the
@server.setRoute()decorator- the decorator requires a route parameter, which is the URI relative to the callback function
@server.setRoute('/')
def root():
return renderFile('index.html')
-
additional parameters can be given to the decorator, such as
methods: [str]which defaults to['GET']- methods must be specified to be accepted by the framework, otherwise it will answer to the client with a
405 Method Now AllowedHTTP response
- methods must be specified to be accepted by the framework, otherwise it will answer to the client with a
regex: boolwhich defaults toFalse- indicates that the route is specified as a regex pattern
allowOptions: boolwhich defaults toTrue- tells the framework if it should answer to
OPTIONSrequests for the given route
- tells the framework if it should answer to
giveMethod: boolwhich defaults toFalse- if set to True, the framework will provide the callback function with the method used by the client
giveUri: boolwhich defaults toFalse- if set to True, the framework will provide the callback function with the uri called by the client
giveRequest: boolwhich defaults toFalse- if set to True, the framework will provide the callback with the request bytes
kwargs: tuple(str)which defaults to()- if the tuple is populated, the framework will expect parameters to be given into the URI
- e.g.
http://HOST:PORT/URI?key=value&other_key=other_value
-
callback functions can either return someting or
None -
allowed return values (different from
None) arestring, which is intended as response bodybytes, which is indended as response bodytuple(headers: dict, responseBody: Any)dict, which is intended as json response bodyResponse- custom class which allows to set HTTP code, response headers and response body
-
for full usage example, take a look at the following code
Usage Example
from blite import Server, Response, renderFile, HttpCode, LogLevel
import json
HOST = '0.0.0.0'
PORT = 8080
server = Server(HOST, PORT)
# set the headers to be used in every HTTP response
server.setStandardHeaders({'Access-Control-Allow-Origin' : '*'})
# sets concise logging level
server.setLogLevel(LogLevel.CONCISE)
@server.setRoute('/')
def root():
renderFile('index.html')
@server.setRoute('/auth', kwargs=('wants_token'), methods=['POST'], allowOptions=False)
def authUser(requestBody, wants_token):
try:
jData = json.loads(requestBody.decode())
except:
return Response(code=HttpCode.NOT_FOUND)
uname = jData.get('username')
passwd = jData.get('password')
if uname is None or passwd is None:
return Response(code=HttpCode.NOT_FOUND)
authenticated = checkCredentials(uname, passwd) # function defined elsewhere
if not authenticated:
return 'failed'
if wants_token == 'true':
token = makeToken(uname, passwd) # function defined elsewhere
return {'token': token}
return 'ok'
@server.setRoute('/get_data', methods=['POST'], giveMethod=True)
def getData(requestBody, method):
try:
jData = json.loads(requestBody.decode())
except:
return Response(code=HttpCode.NOT_FOUND)
authenticated = checkAuthentication(jData) # function defined elsewhere
if not authenticated:
return Response(code=HttpCode.FORBIDDEN)
jsonData = fetchData() # function defined elsewhere, which returns a dict
return jsonData
@server.setRoute('^/get_image/[0-9a-zA-Z]*\.(jpg|png|jpeg)\/?$', regex=True, giveUri=True)
def getImage(uri):
imagePath = findImagePath(uri) # function defined elsewhere
imageBytes = getImageBytes(imagePath) # function defined elsewhere
if not imageBytes:
return Response(code=HttpCode.NOT_FOUND)
imageExtension = imagePath.split('.')[-1]
return {'Content-Type': f'image/{imageExtension}'}, imageBytes
Metadata
Release files for blite 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| blite-1.1.2.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| blite-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.4 kB
Release files / blite-1.1.2.tar.gz
| Download URL | blite-1.1.2.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
50c5080b7f02da2307ce99007d76e8a95edd2a66398adfc4f524c022eb253c55
|
|
BLAKE2b-256 checksum How to use checksums |
07df96feb497ba6981207081c2f0473fb33307ef5a6896bb5b90c6b52d41c70b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / blite-1.1.2-py3-none-any.whl
| Download URL | blite-1.1.2-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
69eb4066972b2154972b807c0ce3b7adc113e40f5bf672a7781260e65cab79cd
|
|
BLAKE2b-256 checksum How to use checksums |
d6b77e4dcc00d1489308c85ac1ca5637236dca7b14362d71e4a9dec420133099
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|