1拒垃、介紹
? 之前我們介紹過(guò)在python中寫(xiě)socket僵朗,當(dāng)然那樣還是太麻煩滋将,有沒(méi)有像pc端apache一樣的支持n多功能衔峰,牛逼的現(xiàn)成服務(wù)器軟件呢佩脊? 這時(shí)MicroWebSrv閃亮登場(chǎng)!5媛薄威彰!
? MicroWebSrv 是一個(gè)在MicroPython平臺(tái)上支持websocket,html/python穴肘,路由功能的微型http服務(wù)器
核心文件是"microWebSrv.py"歇盼,在開(kāi)發(fā)板中引入這個(gè)py文件即可快速搭建自己的webserver服務(wù)器。
2评抚、搭建webserver
倉(cāng)庫(kù)地址:https://github.com/youxinweizhi/MicroPython/tree/master/WebServer/%E4%BB%A3%E7%A0%81/%E5%BF%AB%E9%80%9F%E6%90%AD%E5%BB%BAwebserver
#編輯main.py修改內(nèi)容如下
from microWebSrv import MicroWebSrv
@MicroWebSrv.route('/test/','GET')
def test(httpClient,httpResponse):
t=httpClient.GetRequestMethod()
print(t)
content="""\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
Hello world!
</h1>
</body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
srv = MicroWebSrv(webPath="/")
srv.Start(threaded=True)