简单路由

@app.get(‘/‘)
@app.post(‘/send’)
@app.route(‘/‘)

  1. from sanic import Sanic
  2. from sanic import response
  3. app = Sanic('App_name')
  4. @app.get('/')
  5. async def test(requery):
  6. print('进来了')
  7. return response.json({'code': 200, 'msg': 'ok'})
  8. @app.post('/send')
  9. async def test(requery):
  10. print('进来了')
  11. return response.json({'code': 200, 'msg': 'ok'})
  12. if __name__ =='__main__':
  13. app.run(hot='127.0.0.1', port='8000')

进阶路由

这种路由可以做成分离式 的方式, 业务逻辑为一个文件, 路由的为一个文件

  1. from sanic import Sanic
  2. from sanic import response # 响应
  3. # 按间距中的绿色按钮以运行脚本。
  4. app = Sanic('App_name')
  5. async def iswhen(request):
  6. print('进来了', request.url)
  7. return response.json({'code': 200, 'msg': 'okxin'})
  8. app.add_route(iswhen, "/", methods=['GET', 'POST'], host=['127.0.0.1:8000'])
  9. if __name__ == '__main__':
  10. 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/

  1. from sanic import Sanic
  2. from sanic import response, Blueprint # 响应, 蓝图(多级路由)
  3. app = Sanic('App_name')
  4. # 定义多级路由
  5. api = Blueprint('api', url_prefix='/api')
  6. # 注册蓝图
  7. app.blueprint(api)
  8. @api.route('/')
  9. async def test(request):
  10. print('进来了')
  11. return response.json({'code': 200, 'msg': 'api'})
  12. @api.route('/delapi')
  13. async def test(request):
  14. print('进来了delapi')
  15. return response.json({'code': 200, 'msg': 'delapi'})
  16. if __name__ == '__main__':
  17. app.run(host="127.0.0.1", port=8000)
  1. from sanic import Sanic
  2. from sanic import response, Blueprint # 响应, 蓝图(多级路由)
  3. app = Sanic('App_name')
  4. # 定义多级路由
  5. api = Blueprint('api', url_prefix='/api')
  6. # 注册蓝图
  7. app.blueprint(api)
  8. async def apiAge(request):
  9. print('upapi')
  10. return response.json({'code': 200, 'msg': 'upapi'})
  11. api.add_route(apiAge, '/upapi')
  12. # api.add_route(apiAge, "/api", methods=['GET'], host=['127.0.0.1:8000'])
  13. if __name__ == '__main__':
  14. app.run(host="127.0.0.1", port=8000)

模块化路由

什么是模块化路由? 模块化路由一般用于大型web应用,分类多需要细化, 且方便管理
导航路由为一个单独文件
处理业务逻辑为一个单独文件
image.png

  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: chenchen
  4. @software: PyCharm
  5. @file: main.py
  6. @功能: 模块化路由
  7. @time: 2022-08-24 1:52
  8. """
  9. from sanic import Sanic
  10. from routes import root
  11. app = Sanic(__name__)
  12. app.blueprint(root)
  13. if __name__ == '__main__':
  14. app.run(host='127.0.0.1', port='8000')
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: chenchen
  4. @software: PyCharm
  5. @file: __init__.py
  6. @功能: 引入所有不同分类的路由
  7. @time: 2022-08-24 2:36
  8. """
  9. from sanic import Blueprint
  10. from .authors import authors
  11. from .soft import soft
  12. root = Blueprint.group(authors, soft, url_prefix="/")
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: chenchen
  4. @software: PyCharm
  5. @file: soft.py
  6. @功能: 处理软件的路由文件
  7. @time: 2022-08-24 2:01
  8. """
  9. from sanic import response, Blueprint
  10. from demo_02.controller.soft import soft_index, soft_up
  11. soft = Blueprint('routes_soft', url_prefix='/soft')
  12. soft.add_route(soft_index, '/')
  13. soft.add_route(soft_up, '/up')
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: chenchen
  4. @software: PyCharm
  5. @file: authors.py
  6. @功能: 处理软件的路由文件
  7. @time: 2022-08-24 2:33
  8. """
  9. from sanic import response, Blueprint
  10. from demo_02.controller.authors import authors_index, authors_up
  11. authors = Blueprint('routes_authors', url_prefix='/authors')
  12. authors.add_route(authors_index, '/')
  13. authors.add_route(authors_up, '/up')
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: chenchen
  4. @software: PyCharm
  5. @file: soft.py
  6. @功能: 处理业务逻辑
  7. @time: 2022-08-24 2:26
  8. """
  9. from sanic import response
  10. async def soft_index(requets):
  11. print('soft_index')
  12. return response.json({'code': 200, 'msg': 'soft_index'})
  13. async def soft_up(requets):
  14. print('soft_index')
  15. return response.json({'code': 200, 'msg': 'soft_up'})
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: chenchen
  4. @software: PyCharm
  5. @file: authors.py
  6. @功能: 处理业务逻辑
  7. @time: 2022-08-24 2:26
  8. """
  9. from sanic import response
  10. async def authors_index(requets):
  11. print('soft_index')
  12. return response.json({'code': 200, 'msg': 'soft_index'})
  13. async def authors_up(requets):
  14. print('soft_index')
  15. return response.json({'code': 200, 'msg': 'soft_up'})