A bare bone webserver
Project description
bbwebservice
bbwebservice
is a small Python library for creating simple webservers.
Installation
To install this library, use the pip command: pip install bbwebservice
Usage
- Import:
from bbwebservice.webserver import *
- Register pages for HTTP
GET
:@register
: When using the@register
decorator, you must define the named parametersroute
andtype
.route
: The URL which must be requested via HTTPGET
method for the decorated function to execute. The decorated function is expected to return the respective response content.type
: Thetype
parameter holds the intended MIME-type of the response.
@register(route='/', type=MIME_TYPE.HTML)
def main_page():
return load_file('/content/index.html')
- Register pages for HTTP
POST
:@post_handler
: Works similarly to [1.], with the only difference being that it is mandatory for the decorated function to take a parameter.
@post_handler(route='/makethread', type=MIME_TYPE.HTML)
def makethread(args):
return load_file('/content/post.html')
- Redirect:
To redirect a request, you can return a
Redirect
object which takes the path to which the request should be redirected.
@register(route='/foo', type=MIME_TYPE.HTML)
def redirect():
return Redirect('/')
- PartialContent and videostreaming:
To serve partial-content for video streaming or other applications, you can return a
PartialContent
object which takes the location of the streamed file and the chunk size which determines the size of the parts that should be streamed.
@register(route='/video.mp4', type=MIME_TYPE.MP4)
def vid(args):
return PartialContent("/content/v.mp4", 80000)
- Error handler:
With the
@error_handler
, it is possible to provide default responses for requests with the specified error code.
@error_handler(error_code=404, type=MIME_TYPE.HTML)
def not_found():
return load_file('/content/404.html')
- Handler args: Setting cookies, getting query strings, or setting custom headers are possible when you give your handler function a parameter.
@post_handler(route='/', type=MIME_TYPE.HTML)
def login(args):
set_cookie(args, 'id', "test")
return load_file('/content/index.html')
The server-supplied args
value looks something like this, and changes to the provided value will be reflected in the server answer:
{
"query_string": {},
"flags": [],
"cookies": {},
"address": ("192.168.56.1", 64361),
"post": bytearray(b""),
"request_header": {
"Host": ["192.168.56.1:5000"],
"Connection": ["keep-alive"],
"User-agent": ["Mozilla/5.0 (Windows NT 10.0", "Win64", "x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"],
"Accept": ["text/html,application/xhtml+xml,application/xml", "q=0.9,image/avif,image/webp,image/apng,*/*", "q=0.8,application/signed-exchange", "v=b3", "q=0.7"],
"Accept-encoding": ["gzip, deflate"],
"Accept-language": ["de-DE,de", "q=0.9,en-US", "q=0.8,en", "q=0.7"]
},
"response": "<bbwebservice.http_parser.HTTP_Response object at 0x00000151D5718E50>"
}
- Start the server (example of fully functional server):
To start the server, invoke the
start
function.
from bbwebservice.webserver import *
@register(route='/', type=MIME_TYPE.HTML)
def main():
return load_file('/content/index.html')
start()
Server Configuration
In the directory /config
, there is a file named config.json
. Here you can configure:
{
"ip": "localhost",
"port": 5000,
"queue_size": 10,
"SSL": false,
"cert_path" : "",
"key_path" : ""
}
If you intend to keep the application centrally (online), it is recommended to set the value of ip
to either default
or the IP address of the respective server. Additionally, it is advisable to activate SSL
and set the corresponding paths for cert_path
and key_path
.
Recommended Ports
- 5000: For testing purposes
- 443: For HTTPS (SSL = true)
- 80: For HTTP (SSL = false)
Logging
set_logging(LOGGING_OPTIONS.INFO, True)
set_logging(LOGGING_OPTIONS.DEBUG, True)
set_logging(LOGGING_OPTIONS.ERROR, True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for bbwebservice-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b63befb02cdedf527bc8d63a7b1caba05c063173a47948e8637b94c392ca1ca |
|
MD5 | f5c182b301af021133eb234221a9edb4 |
|
BLAKE2b-256 | 4fe7997645e178705f9c967c6d47b7b28d211f9bc370c0451d2f92f77026a5e1 |