主要使用异步+装饰器的方式
from microdot import Microdot, send_file
创建服务app=Microdot()
启动服务app.run(debug=True)
创建一个网页链接需要使用 装饰器 @app.route(‘/’)
再创建对应函数
async def index(request):
return send_file(‘g.html’)
创建static目录下静态资源访问需要<path:path>
@app.route(‘/static/<path:path>’)
async def static(request, path):
if ‘..’ in path:
# directory traversal is not allowed
return ‘Not found’, 404
return send_file(‘static/’ + path)
GET 和POST 访问需要
@app.route(‘/gp’, methods=[‘GET’,’POST’])
其中get抓取 req.args.get ();post抓取 req.form.get()
async def gp_index(req):
code = req.form.get(‘code’)
回传return
return 1 ,2,3
1可以直接信息,或者send_file发文件,
2是代码 200 302 404 500
3为响应头字典{‘Content-Type’: ‘text/plain; charset=gb2312’}
就是为了简单 所以其他可以暂时不用
app = Microdot()
@app.route('/')
async def index(request):
return send_file('g.html')
@app.route('/static/<path:path>')
async def static(request, path):
if '..' in path:
# directory traversal is not allowed
return 'Not found', 404
return send_file('static/' + path)
@app.route('/gp', methods=['GET'])
async def gp_index(req):
code = req.args.get('code')
print(code)
bk=gp_search(code)
return bk,200, {'Content-Type': 'text/plain; charset=gb2312'}
@app.route('/gpx', methods=['POST'])
async def gp_index(req):
code = req.form.get('code')
print(code)
bk=gp_search(code)
return bk,200, {'Content-Type': 'text/plain; charset=gb2312'}
app.run(debug=True)
创建AP热点
import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)