本文主要是介绍sanic add_route 基本用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码片段
from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import text,json
import urllibclass GeneralView(HTTPMethodView):def get(self, request):result = urllib.parse.parse_qs(request.query_string)result['query_string'] = request.query_stringresult['urlencode_args'] = urllib.parse.urlencode(request.get_args())result['desc'] = '普通 get 请求'return text(str(result))def post(self, request):result = request.get_form()result['desc'] = '普通 post 请求'return json(result)def put(self, request):return text('普通 put 请求')def patch(self, request):return text('普通 patch 请求')def delete(self, request):return text('普通 delete 请求')class AsyncView(HTTPMethodView):async def get(self, request):return text('异步 get 请求')async def post(self, request):return text('异步 post 请求')async def put(self, request):return text('异步 put 请求')async def patch(self, request):return text('异步 patch 请求')async def delete(self, request):return text('异步 delete 请求')app = Sanic('__name__')app.add_route(GeneralView.as_view(), '/general')
app.add_route(AsyncView.as_view(), '/async')if __name__ == '__main__':app.run(host="127.0.0.1", port=8000)
参考
- Sanic框架
- The lightning-fast asynchronous Python web framework
- sanic异步框架使用精解
这篇关于sanic add_route 基本用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!