代码拉取完成,页面将自动刷新
from sanic import Sanic
from sanic.response import json,text
app = Sanic.get_app("myapp")
# @app.get("/")
@app.route('/test', methods=["POST", "PUT"])
async def hello_world(request):
return text("Hello, world.")
#route
@app.route("/handtest")
async def handler(request):
#9种返回类型
return json({"foo": "bar"})
# app.add_route(handler, "/test")
@app.route("/path/to/<foo:str>")
async def handler(request, foo: str):
...
# @app.route("/path/to/<foo:ymd>")
# async def handler(request, foo: datetime.date):
# ...
# /path/to/2021-03-28
# 基于处理程序方法名生成 url 的方法:app.url_for()
'''
@app.route('/')
async def index(request):
# generate a URL for the endpoint `post_handler`
url = app.url_for('post_handler', post_id=5)
# Redirect to `/posts/5`
return redirect(url)
@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
...
'''
#特殊
'''
>> > app.url_for("post_handler", post_id=5, arg_one="one", _anchor="anchor")
'/posts/5?arg_one=one#anchor'
# _external requires you to pass an argument _server or set SERVER_NAME in app.config if not url will be same as no _external
>> > app.url_for("post_handler", post_id=5, arg_one="one", _external=True)
'//server/posts/5?arg_one=one'
# when specifying _scheme, _external must be True
>> > app.url_for("post_handler", post_id=5, arg_one="one", _scheme="http", _external=True)
'http://server/posts/5?arg_one=one'
# you can pass all special arguments at once
>> > app.url_for("post_handler", post_id=5, arg_one=["one", "two"], arg_two=2, _anchor="anchor", _scheme="http",
_external=True, _server="another_server:8888")
'http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor'
'''
#静态文件
'''
app.static("/", "/path/to/index.html")
'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。