| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- from MicroWebSrv.microWebSrv import MicroWebSrv
- # ----------------------------------------------------------------------------
- from module import mod
- @MicroWebSrv.route('/status')
- def _httpHandlerStatusGet(httpClient, httpResponse) :
- content = mod.get_dat()
- print('board status:\n', content)
- httpResponse.WriteResponseOk( headers = None,
- contentType = "text/html",
- contentCharset = "UTF-8",
- content = content )
- @MicroWebSrv.route('/test')
- def _httpHandlerTestGet(httpClient, httpResponse) :
- httpResponse.WriteResponseFile(
- filepath= r'MicroWebSrv\web_files\test.html',
- contentType = "text/html",
- headers = None)
- print('handle')
- # print('GetRequestHeaders:',httpClient.GetRequestHeaders())
- # print('ReadRequestContent: ',httpClient.ReadRequestContent(size=None))
- # print('ReadRequestContentAsJSON:', httpClient.ReadRequestContentAsJSON())
- @MicroWebSrv.route('/test', 'POST')
- def _httpHandlerTestPost(httpClient, httpResponse) :
- formData = httpClient.ReadRequestPostedFormData()
- firstname = formData["firstname"]
- lastname = formData["lastname"]
- content = """\
- <!DOCTYPE html>
- <html lang=en>
- <head>
- <meta charset="UTF-8" />
- <title>TEST POST</title>
- </head>
- <body>
- <h1>TEST POST</h1>
- Firstname = %s<br />
- Lastname = %s<br />
- </body>
- </html>
- """ % ( MicroWebSrv.HTMLEscape(firstname),
- MicroWebSrv.HTMLEscape(lastname) )
- httpResponse.WriteResponseOk( headers = None,
- contentType = "text/html",
- contentCharset = "UTF-8",
- content = content )
- @MicroWebSrv.route('/edit/<index>') # <IP>/edit/123 -> args['index']=123
- @MicroWebSrv.route('/edit/<index>/abc/<foo>') # <IP>/edit/123/abc/bar -> args['index']=123 args['foo']='bar'
- @MicroWebSrv.route('/edit') # <IP>/edit -> args={}
- def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) :
- content = """\
- <!DOCTYPE html>
- <html lang=en>
- <head>
- <meta charset="UTF-8" />
- <title>TEST EDIT</title>
- </head>
- <body>
- """
- content += "<h1>EDIT item with {} variable arguments</h1>"\
- .format(len(args))
-
- if 'index' in args :
- content += "<p>index = {}</p>".format(args['index'])
-
- if 'foo' in args :
- content += "<p>foo = {}</p>".format(args['foo'])
-
- content += """
- </body>
- </html>
- """
- httpResponse.WriteResponseOk( headers = None,
- contentType = "text/html",
- contentCharset = "UTF-8",
- content = content )
- # ----------------------------------------------------------------------------
- def _acceptWebSocketCallback(webSocket, httpClient) :
- print("WS ACCEPT")
- webSocket.RecvTextCallback = _recvTextCallback
- webSocket.RecvBinaryCallback = _recvBinaryCallback
- webSocket.ClosedCallback = _closedCallback
- def _recvTextCallback(webSocket, msg) :
- print("WS RECV TEXT : %s" % msg)
- webSocket.SendText("Reply for %s" % msg)
- def _recvBinaryCallback(webSocket, data) :
- print("WS RECV DATA : %s" % data)
- def _closedCallback(webSocket) :
- print("WS CLOSED")
- # ----------------------------------------------------------------------------
- routeHandlers = [
- # ( "/", "GET", _httpHandlerRootGet ),
- # ( "/js", "GET", _httpHandlerJsGet ),
- # ( "/css/<index>", "GET", _httpHandlerCssGet ),
- # ( "/img", "GET", _httpHandlerImgGet ),
- ( "/status", "GET", _httpHandlerStatusGet ),
- ( "/test", "GET", _httpHandlerTestGet ),
- ( "/test", "POST", _httpHandlerTestPost )
- ]
|