首页 > 其他分享 >Sanic学习之helloworld

Sanic学习之helloworld

时间:2022-10-08 18:02:38浏览次数:48  
标签:__ name app sanic helloworld 学习 Sanic hello

#text文本输出
from sanic import Sanic from sanic.response import text app = Sanic(name="App") @app.route("/hello") async def hello(request): return text("hello,world") if __name__ == '__main__': app.run(port=8000) #可自定义地址:host='127.0.0.0'

#访问localhost:8000/hello
def 方法名不能为 test()
app = Sanic(name='app') #name需要填写
不然会报错:sanic.exceptions.SanicException: Sanic instance cannot be unnamed. Please use Sanic(name='your_application_name') instead.
还有就是
@app.route("/hello") 可以
@app.get("/hello") 也行
具体可移步看:https://www.cnblogs.com/zhongyehai/p/15091815.html
# json输出
from sanic import Sanic
from sanic.response import json

app = Sanic(name="APP")

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

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

标签:__,name,app,sanic,helloworld,学习,Sanic,hello
From: https://www.cnblogs.com/lingsong/p/16769744.html

相关文章