zoukankan      html  css  js  c++  java
  • Sanic官翻-静态文件

    静态文件

    Sanic在使用app.static()方法注册时会提供静态文件和目录,例如图像文件。该方法采用端点URL和文件名。然后可以通过给定的端点访问指定的文件

    from sanic import Sanic
    from sanic.blueprints import Blueprint
    
    app = Sanic(__name__)
    
    # Serves files from the static folder to the URL /static
    app.static('/static', './static')
    # use url_for to build the url, name defaults to 'static' and can be ignored
    app.url_for('static', filename='file.txt') == '/static/file.txt'
    app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
    
    # Serves the file /home/ubuntu/test.png when the URL /the_best.png
    # is requested
    app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
    
    # you can use url_for to build the static file url
    # you can ignore name and filename parameters if you don't define it
    app.url_for('static', name='best_png') == '/the_best.png'
    app.url_for('static', name='best_png', filename='any') == '/the_best.png'
    
    # you need define the name for other static files
    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'
    
    # also, you can use static for blueprint
    bp = Blueprint('bp', url_prefix='/bp')
    bp.static('/static', './static')
    
    # specify a different content_type for your files
    # such as adding 'charset'
    app.static('/', '/public/index.html', content_type="text/html; charset=utf-8")
    
    # servers the file directly
    bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
    app.blueprint(bp)
    
    app.url_for('static', name='bp.static', filename='file.txt') == '/bp/static/file.txt'
    app.url_for('static', name='bp.best_png') == '/bp/test_best.png'
    
    app.run(host="0.0.0.0", port=8000)
    

    注意:为静态目录提供服务时,Sanic不提供目录索引。

    虚拟主机

    app.static()方法还支持虚拟主机。您可以通过host参数将静态文件与特定的虚拟主机一起使用。例如:

    from sanic import Sanic
    
    app = Sanic(__name__)
    
    app.static('/static', './static')
    app.static('/example_static', './example_static', host='www.example.com')
    

    流大文件

    在某些情况下,您可以使用Sanic服务器处理大文件(例如,视频,图像等)。您可以选择使用流式传输文件,而不是直接下载。

    from sanic import Sanic
    
    app = Sanic(__name__)
    
    app.static('/large_video.mp4', '/home/ubuntu/large_video.mp4', stream_large_files=True)
    

    stream_large_filesTrue时,Sanic将使用file_stream()而不是file()来提供静态文件。这将使用1KB作为默认块大小。而且,如果需要,您还可以使用自定义块大小。例如:

    from sanic import Sanic
    
    app = Sanic(__name__)
    
    chunk_size = 1024 * 1024 * 8 # Set chunk size to 8KB
    app.static('/large_video.mp4', '/home/ubuntu/large_video.mp4', stream_large_files=chunk_size)
    
  • 相关阅读:
    控件的用法ComboBox;RadionButton;Timer;DataGridView
    echo.js实现图片延迟加载,
    取消移动端a标签点击变色
    JS实现上传图片实时预览
    Myeclipse6.0安装svn插件
    警告: Parameters: Character decoding failed. Parameter skipped
    jsp到action 传递url时中文出现乱码
    java面试题
    myeclipse中hibernate出错
    java中abstract的用法
  • 原文地址:https://www.cnblogs.com/fhkankan/p/14763516.html
Copyright © 2011-2022 走看看