简单路由
@app.get(‘/‘)
@app.post(‘/send’)
@app.route(‘/‘)
from sanic import Sanicfrom sanic import responseapp = Sanic('App_name')@app.get('/')async def test(requery):print('进来了')return response.json({'code': 200, 'msg': 'ok'})@app.post('/send')async def test(requery):print('进来了')return response.json({'code': 200, 'msg': 'ok'})if __name__ =='__main__':app.run(hot='127.0.0.1', port='8000')
进阶路由
这种路由可以做成分离式 的方式, 业务逻辑为一个文件, 路由的为一个文件
from sanic import Sanicfrom sanic import response # 响应# 按间距中的绿色按钮以运行脚本。app = Sanic('App_name')async def iswhen(request):print('进来了', request.url)return response.json({'code': 200, 'msg': 'okxin'})app.add_route(iswhen, "/", methods=['GET', 'POST'], host=['127.0.0.1:8000'])if __name__ == '__main__':app.run(host="127.0.0.1", port=8000)
多级路由(蓝图)
多级路由的方式有2种
http://127.0.0.1:8000/api/
http://127.0.0.1:8000/api/delapi/
http://127.0.0.1:8000/api/upapi/
from sanic import Sanicfrom sanic import response, Blueprint # 响应, 蓝图(多级路由)app = Sanic('App_name')# 定义多级路由api = Blueprint('api', url_prefix='/api')# 注册蓝图app.blueprint(api)@api.route('/')async def test(request):print('进来了')return response.json({'code': 200, 'msg': 'api'})@api.route('/delapi')async def test(request):print('进来了delapi')return response.json({'code': 200, 'msg': 'delapi'})if __name__ == '__main__':app.run(host="127.0.0.1", port=8000)
from sanic import Sanicfrom sanic import response, Blueprint # 响应, 蓝图(多级路由)app = Sanic('App_name')# 定义多级路由api = Blueprint('api', url_prefix='/api')# 注册蓝图app.blueprint(api)async def apiAge(request):print('upapi')return response.json({'code': 200, 'msg': 'upapi'})api.add_route(apiAge, '/upapi')# api.add_route(apiAge, "/api", methods=['GET'], host=['127.0.0.1:8000'])if __name__ == '__main__':app.run(host="127.0.0.1", port=8000)
模块化路由
什么是模块化路由? 模块化路由一般用于大型web应用,分类多需要细化, 且方便管理
导航路由为一个单独文件
处理业务逻辑为一个单独文件
# -*- coding: utf-8 -*-"""@author: chenchen@software: PyCharm@file: main.py@功能: 模块化路由@time: 2022-08-24 1:52"""from sanic import Sanicfrom routes import rootapp = Sanic(__name__)app.blueprint(root)if __name__ == '__main__':app.run(host='127.0.0.1', port='8000')
# -*- coding: utf-8 -*-"""@author: chenchen@software: PyCharm@file: __init__.py@功能: 引入所有不同分类的路由@time: 2022-08-24 2:36"""from sanic import Blueprintfrom .authors import authorsfrom .soft import softroot = Blueprint.group(authors, soft, url_prefix="/")
# -*- coding: utf-8 -*-"""@author: chenchen@software: PyCharm@file: soft.py@功能: 处理软件的路由文件@time: 2022-08-24 2:01"""from sanic import response, Blueprintfrom demo_02.controller.soft import soft_index, soft_upsoft = Blueprint('routes_soft', url_prefix='/soft')soft.add_route(soft_index, '/')soft.add_route(soft_up, '/up')
# -*- coding: utf-8 -*-"""@author: chenchen@software: PyCharm@file: authors.py@功能: 处理软件的路由文件@time: 2022-08-24 2:33"""from sanic import response, Blueprintfrom demo_02.controller.authors import authors_index, authors_upauthors = Blueprint('routes_authors', url_prefix='/authors')authors.add_route(authors_index, '/')authors.add_route(authors_up, '/up')
# -*- coding: utf-8 -*-"""@author: chenchen@software: PyCharm@file: soft.py@功能: 处理业务逻辑@time: 2022-08-24 2:26"""from sanic import responseasync def soft_index(requets):print('soft_index')return response.json({'code': 200, 'msg': 'soft_index'})async def soft_up(requets):print('soft_index')return response.json({'code': 200, 'msg': 'soft_up'})
# -*- coding: utf-8 -*-"""@author: chenchen@software: PyCharm@file: authors.py@功能: 处理业务逻辑@time: 2022-08-24 2:26"""from sanic import responseasync def authors_index(requets):print('soft_index')return response.json({'code': 200, 'msg': 'soft_index'})async def authors_up(requets):print('soft_index')return response.json({'code': 200, 'msg': 'soft_up'})
