urls.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from MicroWebSrv.microWebSrv import MicroWebSrv
  2. # ----------------------------------------------------------------------------
  3. from module import mod
  4. @MicroWebSrv.route('/status')
  5. def _httpHandlerStatusGet(httpClient, httpResponse) :
  6. content = mod.get_dat()
  7. print('board status:\n', content)
  8. httpResponse.WriteResponseOk( headers = None,
  9. contentType = "text/html",
  10. contentCharset = "UTF-8",
  11. content = content )
  12. @MicroWebSrv.route('/test')
  13. def _httpHandlerTestGet(httpClient, httpResponse) :
  14. httpResponse.WriteResponseFile(
  15. filepath= r'MicroWebSrv\web_files\test.html',
  16. contentType = "text/html",
  17. headers = None)
  18. print('handle')
  19. # print('GetRequestHeaders:',httpClient.GetRequestHeaders())
  20. # print('ReadRequestContent: ',httpClient.ReadRequestContent(size=None))
  21. # print('ReadRequestContentAsJSON:', httpClient.ReadRequestContentAsJSON())
  22. @MicroWebSrv.route('/test', 'POST')
  23. def _httpHandlerTestPost(httpClient, httpResponse) :
  24. formData = httpClient.ReadRequestPostedFormData()
  25. firstname = formData["firstname"]
  26. lastname = formData["lastname"]
  27. content = """\
  28. <!DOCTYPE html>
  29. <html lang=en>
  30. <head>
  31. <meta charset="UTF-8" />
  32. <title>TEST POST</title>
  33. </head>
  34. <body>
  35. <h1>TEST POST</h1>
  36. Firstname = %s<br />
  37. Lastname = %s<br />
  38. </body>
  39. </html>
  40. """ % ( MicroWebSrv.HTMLEscape(firstname),
  41. MicroWebSrv.HTMLEscape(lastname) )
  42. httpResponse.WriteResponseOk( headers = None,
  43. contentType = "text/html",
  44. contentCharset = "UTF-8",
  45. content = content )
  46. @MicroWebSrv.route('/edit/<index>') # <IP>/edit/123 -> args['index']=123
  47. @MicroWebSrv.route('/edit/<index>/abc/<foo>') # <IP>/edit/123/abc/bar -> args['index']=123 args['foo']='bar'
  48. @MicroWebSrv.route('/edit') # <IP>/edit -> args={}
  49. def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) :
  50. content = """\
  51. <!DOCTYPE html>
  52. <html lang=en>
  53. <head>
  54. <meta charset="UTF-8" />
  55. <title>TEST EDIT</title>
  56. </head>
  57. <body>
  58. """
  59. content += "<h1>EDIT item with {} variable arguments</h1>"\
  60. .format(len(args))
  61. if 'index' in args :
  62. content += "<p>index = {}</p>".format(args['index'])
  63. if 'foo' in args :
  64. content += "<p>foo = {}</p>".format(args['foo'])
  65. content += """
  66. </body>
  67. </html>
  68. """
  69. httpResponse.WriteResponseOk( headers = None,
  70. contentType = "text/html",
  71. contentCharset = "UTF-8",
  72. content = content )
  73. # ----------------------------------------------------------------------------
  74. def _acceptWebSocketCallback(webSocket, httpClient) :
  75. print("WS ACCEPT")
  76. webSocket.RecvTextCallback = _recvTextCallback
  77. webSocket.RecvBinaryCallback = _recvBinaryCallback
  78. webSocket.ClosedCallback = _closedCallback
  79. def _recvTextCallback(webSocket, msg) :
  80. print("WS RECV TEXT : %s" % msg)
  81. webSocket.SendText("Reply for %s" % msg)
  82. def _recvBinaryCallback(webSocket, data) :
  83. print("WS RECV DATA : %s" % data)
  84. def _closedCallback(webSocket) :
  85. print("WS CLOSED")
  86. # ----------------------------------------------------------------------------
  87. routeHandlers = [
  88. # ( "/", "GET", _httpHandlerRootGet ),
  89. # ( "/js", "GET", _httpHandlerJsGet ),
  90. # ( "/css/<index>", "GET", _httpHandlerCssGet ),
  91. # ( "/img", "GET", _httpHandlerImgGet ),
  92. ( "/status", "GET", _httpHandlerStatusGet ),
  93. ( "/test", "GET", _httpHandlerTestGet ),
  94. ( "/test", "POST", _httpHandlerTestPost )
  95. ]