zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Sanic框架安装与简单用法

安装框架 简单 用法
2023-09-14 09:00:32 时间

###

sanic官方文档:https://sanic.readthedocs.io/en/latest/

###

看看官方文档的介绍

Sanic is a Python 3.7+ web server and web framework that’s written to go fast.

It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.

###

Sanic是一个类似Flask的Python 3.7+ Web服务器,它的写入速度非常快。除了Flask之外,Sanic还支持异步请求处理程序。这意味着你可以使用Python 3.5中新的闪亮的异步/等待语法,使你的代码非阻塞和快速。

Sanic最低支持Python 3.7,如果需要学习Sanic,请先下载版本不低于3.7的Python包

###

安装Sanic

pip3 install sanic

注意:Sanic暂时只能在mac os系统和linux系统下安装,windows系统暂不支持

###

一个简单的Sanic小程序

Hello World Example

from sanic import Sanic
from sanic.response import json

app = Sanic("My Hello, world app")

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run()

 

运行manager.py文件,就可以运行起来一个简单的由Sanic开发web小程序了,我们可以在浏览器输入http://localhost:5000/就可以访问了!

用惯Flask的同学,会发现Sanic的用法与Flask有点类似,

####

 

####