Ilya_Bykonya

Untitled

Jul 4th, 2024
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | Source Code | 0 0
  1. from microdot import Microdot, Response, send_file
  2. from utemplate import recompile
  3. import requests
  4. import uasyncio
  5. import uaioweb
  6. import network
  7. import machine
  8. import urandom
  9. import utime
  10. import ujson
  11. import usys
  12.  
  13.  
  14. app = Microdot()
  15. Response.default_content_type = 'text/html'
  16.  
  17. value = 42
  18. @app.route('/value')
  19. async def read(request):
  20.     print("Receive Get Request!")
  21.     return '42'
  22. @app.route('/value/toggle')
  23. async def toggle(request):
  24.     print("Receive Toggle Request!")
  25.     global value
  26.     value = 228 if value == 42 else 42
  27.     return "OK"
  28.  
  29.  
  30. flag = False
  31. loader = recompile.Loader(None, 'templates')
  32. @app.route('/')
  33. async def index(request):
  34.     global flag
  35.     global loader
  36.     return loader.load('index.html')(led_value = flag)
  37. @app.route('/toggle')
  38. async def toggle_led(request):
  39.     global flag
  40.     flag = False if flag else True
  41.     return "OK"
  42.  
  43.  
  44.  
  45.  
  46. @app.route('/shutdown')
  47. async def shutdown(request):
  48.     request.app.shutdown()
  49.     return 'The server is shutting down...'
  50. @app.route('/static/<path:path>')
  51. def static(request, path):
  52.     if '..' in path:
  53.         # directory traversal is not allowed
  54.         return 'Not found', 404
  55.     return send_file('static/' + path)
  56. async def infinity_print(message: str, timeout_ms: int = 2500):
  57.     while True:
  58.         print(message)
  59.         await uasyncio.sleep_ms(2500)
  60.  
  61. uasyncio.run(uasyncio.gather(
  62.     app.start_server(host = '0.0.0.0', port = 8090, debug = True),
  63.     infinity_print('he-he', 5000)
  64. ))
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment