from microWebSrv import MicroWebSrv # ---------------------------------------------------------------------------- @MicroWebSrv.route('/test') def _httpHandlerTestGet(httpClient, httpResponse) : content = """\ TEST GET

TEST GET

Client IP address = %s
First name:
Last name:
""" % httpClient.GetIPAddr() httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) @MicroWebSrv.route('/test', 'POST') def _httpHandlerTestPost(httpClient, httpResponse) : formData = httpClient.ReadRequestPostedFormData() firstname = formData["firstname"] lastname = formData["lastname"] content = """\ TEST POST

TEST POST

Firstname = %s
Lastname = %s
""" % ( MicroWebSrv.HTMLEscape(firstname), MicroWebSrv.HTMLEscape(lastname) ) httpResponse.WriteResponseOk( headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content ) @MicroWebSrv.route('/edit/') # /edit/123 -> args['index']=123 @MicroWebSrv.route('/edit//abc/') # /edit/123/abc/bar -> args['index']=123 args['foo']='bar' @MicroWebSrv.route('/edit') # /edit -> args={} def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) : content = """\ TEST EDIT """ content += "

EDIT item with {} variable arguments

"\ .format(len(args)) if 'index' in args : content += "

index = {}

".format(args['index']) if 'foo' in args : content += "

foo = {}

".format(args['foo']) content += """ """ 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 = [ # ( "/test", "GET", _httpHandlerTestGet ), # ( "/test", "POST", _httpHandlerTestPost ) #] srv = MicroWebSrv(webPath='/web_root/web_admin/') srv.MaxWebSocketRecvLen = 256 srv.WebSocketThreaded = False srv.AcceptWebSocketCallback = _acceptWebSocketCallback srv.Start() # ----------------------------------------------------------------------------