zoukankan      html  css  js  c++  java
  • sanic官方文档解析之静态文件和版本

    1,静态文件

    就向图片文件一样,静态文件和指导性的文件,当通过Sanic服务端用app.static()方法注册的时候,这种方法采用端点url和文件名称获得.这样的文件的指定,将会通过指定的端点访问.

    from sanic import Sanic
    from sanic.blueprints import Blueprint
    
    # 实例化一个Sanic对象
    app = Sanic(__name__)
    
    # 从/static的静态文件中获取静态文件
    app.static("/static", "/static")
    # 使用url_for来建立url.默认是static的命名可以被忽略
    app.url_for("static", filename="file.text") == "/static/file.text"
    app.url_for("static", name="static", filename="file.text") == "/static/file.text"
    
    # 当类似/the_best.png这样的url被请求的时候,我们可以提供/home/ubuntu/test.png这样路径下的文件
    app.static("/the_best.png", "/home/ubuntu/test.png", name="best_png")
    
    # 你也可以使用url_for来建立静态文件的url
    # 如果你不想定义静态文件的名称,你也可以不写
    app.url_for("static", name="best_png") == "/the_best.png"
    app.url_for("static", name="best_png", filename="any") == "/the_best.png"
    
    # 你需要为其他静态文件命名
    app.static("/another.png", "/home/ubuntu/another.png", name="another")
    app.url_for("static", name="another") == "/another.png"
    app.url_for("static", name="another", filename="any") == "/another.png"
    
    # 你也可以为蓝图使用静态文件
    bq = Blueprint("bq", url_prefix="/bq")
    bq.static("/static", "/static")
    # 直接提供文件
    bq.static("/the_best.png", "/home/ubuntn/test.png", name="best_png")
    app.blueprint(bq)
    
    app.url_for("static", name="bq.static", filename="file.text") == "/bq/static/file.text"
    app.url_for("static", name="bq.best_png") == "/bq/test_best.png"
    
    app.run(host="0.0.0.0", port=8000, debug=True)

    注意:当提供静态目录的时候,sanic是不提供有序目录的.

    • 1.1虚拟主机

    app.static()同时也支持虚拟主机,你可以用特殊的虚拟主机(有参数的),提供静态文件,如上图所示.

    • 1.2流式处理大文件

    在某些情况下,你也会用sanic提供向视频,图片等文件,你可以选择流式文件要好,相比较于直接下载

    stream_large_files=True的时候,Sanic会使用file_stream()放法来替代file()来提供sanic文件,这样作为默认的块的大小,这样才使用3kb左右的大小,如果有需要,你也可以自定义块的大小.

    2,版本

    你可以通过version版本来装饰路由,或者初始化一个蓝图,,如果用了version,就会造成一个v{verdion}的url前缀,当{version}在版本里是一个数字的地方

    • 2.1,每一个路由

    你也可以通过路由直接设置版本号,如上图所示.

    我也不知道curl有个啥用???

    • 2,全局蓝图版本

    也 可以在实例化蓝图的时候,把版本号存放于蓝图的对象中,这样这个蓝图对象的所有路由都会携带版本号.

  • 相关阅读:
    [Luogu5042/UOJ #100][国家集训队互测2015]丢失的题面/ydc的题面
    [51nod1773]A国的贸易
    [GZOI2019&GXOI2019]省选GG记
    [51nod1659]数方块
    [51nod1052]最大M子段和
    [51nod1201]整数划分
    [51nod1084]矩阵取数问题 V2
    [51nod1020]逆序排列
    [BZOJ3000]Big Number
    [BZOJ1684][Usaco2005 Oct]Close Encounter
  • 原文地址:https://www.cnblogs.com/ljc-0923/p/10391805.html
Copyright © 2011-2022 走看看