|
|
@@ -1,144 +0,0 @@
|
|
|
-from microWebSrv import MicroWebSrv
|
|
|
-# ----------------------------------------------------------------------------
|
|
|
-@MicroWebSrv.route('/')
|
|
|
-def _httpHandlerRootGet(httpClient, httpResponse) :
|
|
|
- print('get root')
|
|
|
- httpResponse.WriteResponseFile(
|
|
|
- filepath= r'MicroWebSrv\web_files\index.html',
|
|
|
- contentType = "text/html",
|
|
|
- headers = None)
|
|
|
-
|
|
|
-@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 ),
|
|
|
- ( "/test", "GET", _httpHandlerTestGet ),
|
|
|
- ( "/test", "POST", _httpHandlerTestPost )
|
|
|
-]
|
|
|
-'''
|
|
|
-` 实例化web服务器对象
|
|
|
-` bindIP='192.168.31.123' 必填。在WiFi已经连接路由器的情况下,填写esp32的ip地址
|
|
|
-` 这个脚本同时支持 Python3.10 代码,测试时填上电脑本机IP地址即可
|
|
|
-` routeHandlers 这是web网页的路由表
|
|
|
- - 当浏览器访问 192.168.31.123/test 时,会调用 _httpHandlerTestGet 函数
|
|
|
- - 当浏览器点击 summit 时,
|
|
|
- - 如 _httpHandlerTestGet 函数中 content 的 html 内容 method="post" 所显示的那样:<form action="/test" method="post" accept-charset="ISO-8859-1">
|
|
|
- - 该语句会以 POST 方式请求服务器
|
|
|
- - 再经过 routeHandlers 路由到 _httpHandlerTestPost 回调函数,输出函数内 content 的结果返回给浏览器
|
|
|
-` 浏览器直接输入: 192.168.43.67 会出现 index.html 页面(前提是 webPath='MicroWebSrv/www/' 路径设置正常)
|
|
|
- - index.html 文件在哪路由?
|
|
|
- - 在 MicroWebSrv\microWebSrv.py 文件中,初始化提到:_indexPages,函数 _physPathFromURLPath 调用相关页面
|
|
|
- - 理论上这是作者源文件里面的东西,我们可以不用管它,也无法从外部修改,直接使用 WriteResponseFile 函数返回网页文件即可
|
|
|
- - `httpResponse.WriteResponseFile(filepath= '/my-html.html',contentType = "text/html",headers = None)`
|
|
|
-'''
|
|
|
-def main():
|
|
|
- import sys
|
|
|
- if sys.platform == 'win32':
|
|
|
- ip_addr = '192.168.43.240'
|
|
|
- web_path = 'MicroWebSrv\web_files'
|
|
|
- else:
|
|
|
- from mywifi import my_wifi
|
|
|
- ip_addr = my_wifi.get_wifi_addr()
|
|
|
- web_path = '/MicroWebSrv/web_files'
|
|
|
- print(web_path)
|
|
|
- srv = MicroWebSrv(routeHandlers=routeHandlers, bindIP=ip_addr, webPath='MicroWebSrv\web_files')
|
|
|
- srv.MaxWebSocketRecvLen = 256
|
|
|
- srv.WebSocketThreaded = True
|
|
|
- srv.AcceptWebSocketCallback = _acceptWebSocketCallback
|
|
|
- srv.SetNotFoundPageUrl("https://www.baidu.com/")
|
|
|
- srv.Start(threaded=True)
|
|
|
- print("start:", ip_addr)
|
|
|
-
|
|
|
- if sys.platform == 'win32':
|
|
|
- while True:
|
|
|
- pass
|
|
|
-if __name__ == '__main__':
|
|
|
- main()
|
|
|
-
|
|
|
-# ----------------------------------------------------------------------------
|