Skip to main content

Minimalistic core for webserver on microcontroler

Project description

The catch

The first start for every webserver is routing and parsing html. This is the core.

Routing

Router objects are simple and are input to the server instances. Example:

from userv.routing import Router, text_response

router = Router()

def test(request):
    return text_response("some text")

router.add("/resturl", test, method="GET")

you can get function test again with:

router.get('/resturl', method="GET")

for creating swagger and so on there is a also a list command:

router.list()

With these routes in one place we have full control even without an server instance.

Response types

There are a few build in response types text, json and static files.

The general gist of the response functions is to create a generator which will be used to consumed by your server.

if you want to have full control about your memory flow or need to write a few memory hungry responses feel free to write an response yourself.

Little hint make sure to use the response_header function and end the response with an “\r\n”.

Serve static files

It is pretty simple just add the address with the file you want to serve.

Example:

from userv.routing import static_file

router.add("/index", static_file('boot.py'))

Although the example should never expose your code. It is a pretty simple and fast test.

Swagger your api

swagger.io is the a good way to document your api. This section is a quick way to serve an swagger.json from your server. The view of this api has to be done in your local network.

Example for description:

# Get requests have a parameter description
@swagger.info("My_funny summary")
@swagger.parameter('myvarname', description="", example='sdfs', required=True)
@swagger.response(200, 'smth is off')
def myrest_func(request):
    raise


# Post request needs a body description
@swagger.info("My_funny summary")
@swagger.body('weatherinfo', {'tada': "examplevar",
                              "tada2": 2})
@swagger.response(200, 'smth is off')
def post_myrest_func(request):
    raise

router.add("/resturl", myrest_func, method="GET")
router.add("/resturl", post_myrest_func, method="POST")

Example to serve swagger.json:

from userv.swagger import swagger_file
router.add("/swagger.json", swagger_file('my swagger api', "api title", router_instance=router))

the example shows we add the router_instance and therefore all routes registered to that point. meaning if you want all routes in your swagger add the swagger_file shortly before running. All you need to do now is open an swagger ui in your browser and use the link to your swagger you serve.

Further packages & webserver

Atm there is an implementation for an socketserver which runs even on on esp8266:

pip install userv.socket_server

And an async server with an exchangable interface.:

pip install userv.async_server

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

userv-0.6.0.tar.gz (7.5 kB view hashes)

Uploaded Source

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