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. To view the api you can embed a swagger_index.html with cdn´s.

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, swagger_index
router.add("/rest/swagger.json", swagger_file('my swagger api', "api title", router_instance=router))

router.add("/rest/index.html, swagger_index(host="127.0.0.1", swagger_json_url="rest/swagger.json")

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.

Now all you need to do is set proper host adresses and call up the index url in one or your browsers. Because this is IoT always have in mind qr-codes and other links are easy to create and glue on your device.

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.7.1.tar.gz (8.3 kB view details)

Uploaded Source

File details

Details for the file userv-0.7.1.tar.gz.

File metadata

  • Download URL: userv-0.7.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for userv-0.7.1.tar.gz
Algorithm Hash digest
SHA256 9e381faa2a7734cdd6dfb436a604d30abdb2c1d44a854e993d54060be2ae5abb
MD5 2ea2b0d3485ae94c4083c266c9247ac4
BLAKE2b-256 eb50169d3d0c246350da92e49665e388ad8218c47336bbe465275d018c5e57b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page