Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from microdot import Microdot, Response, send_file
- from utemplate import recompile
- import requests
- import uasyncio
- import uaioweb
- import network
- import machine
- import urandom
- import utime
- import ujson
- import usys
- app = Microdot()
- Response.default_content_type = 'text/html'
- value = 42
- @app.route('/value')
- async def read(request):
- print("Receive Get Request!")
- return '42'
- @app.route('/value/toggle')
- async def toggle(request):
- print("Receive Toggle Request!")
- global value
- value = 228 if value == 42 else 42
- return "OK"
- flag = False
- loader = recompile.Loader(None, 'templates')
- @app.route('/')
- async def index(request):
- global flag
- global loader
- return loader.load('index.html')(led_value = flag)
- @app.route('/toggle')
- async def toggle_led(request):
- global flag
- flag = False if flag else True
- return "OK"
- @app.route('/shutdown')
- async def shutdown(request):
- request.app.shutdown()
- return 'The server is shutting down...'
- @app.route('/static/<path:path>')
- def static(request, path):
- if '..' in path:
- # directory traversal is not allowed
- return 'Not found', 404
- return send_file('static/' + path)
- async def infinity_print(message: str, timeout_ms: int = 2500):
- while True:
- print(message)
- await uasyncio.sleep_ms(2500)
- uasyncio.run(uasyncio.gather(
- app.start_server(host = '0.0.0.0', port = 8090, debug = True),
- infinity_print('he-he', 5000)
- ))
Advertisement
Add Comment
Please, Sign In to add comment