python sanic视频_Python Web框架Sanic 静态文件
我们在写web app(网站)的时候会用到很多静态文件,比如css,JavaScript,图片等,这些文件及其文件夹可以通过
app.static()
方法注册,从而被访问到。该方法有两个必需参数,节点URL和文件名。
from sanic import Sanic
from sanic.blueprints import Blueprint
app = Sanic(__name__)
# 提供文件夹`static`里面的文件到URL `/static`的访问。
app.static('/static', './static')
# 使用`url_for`创建URL时,默认为'static'的`name`可被省略。
app.url_for('static', filename='file.txt') == '/static/file.txt'
app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
# 通过URL /the_best.png访问文件 /home/ubuntu/test.png
app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
# 通过url_for创建静态文件URL
# 如果没有定义name 和 filename 参数时可以省略它们。
# 如果上面没有定义name='best_png',则下面的url_for可省略name参数
app.url_for('static', name='best_png') == '/the_best.png'
app.url_for('static', name='best_png', filename='any') == '/the_best.png'
# 需要为其它静态文件定义`name`
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'
# 同样, 也可以对blueprint使用`static`
bp = Blueprint('bp', url_prefix='/bp')
bp.static('/static', './static')
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)
对blueprint使用
url_for
创建的URL会加上
/bp
前缀。
注意:
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_files
为
True
时,Sanic将会使用
file_stream()
替代
file()
来提供静态文件访问。它默认的块大小是
1KB
,当然你可以根据需要修改块大小。比如:
from sanic import Sanic
app = Sanic(__name__)
chunk_size = 1024 * 1024 * 8 # 设置块大小为 8KB
app.static('/large_video.mp4', '/home/ubuntu/large_video.mp4', stream_large_files=chunk_size)
python sanic视频_Python Web框架Sanic 静态文件相关推荐
- python 通信中间件_Python Web框架Sanic middleware – 中间件
中间件是在服务器接受请求之前或之后执行的函数.它们用于修改传递给路由处理函数的 request ,或是由处理函数生成的 response 对象. 中间件类型 中间件有两种类型: request 和 r ...
- 自定义python框架_Python web 框架Sanic 学习: 自定义 Exception
Sanic 是一个和类Flask 的基于Python3.5+的web框架,它使用了 Python3 异步特性,有远超 flask 的性能. 编写 RESTful API 的时候,我们会定义特定的异常错 ...
- python bottle框架 重定向_Python的web框架bottle静态文件的路径
这几天想用bottle来做一个简单的基于web页面的小应用,在调用显示静态文件时被路径卡了半天,现在把问题和解决办法写出来备用和分享给有需要的人. 先上代码: from bottle import s ...
- python 视图对象_python web框架篇:views视图函数
Django请求的生命周期是怎样的? 简单地说,通过URL对应关系匹配 ->找到对应的函数(或者类)->返回字符串(或者读取Html之后返回渲染的字符串) 解剖起来如下: 1. 当用户在浏 ...
- Python学习教程(Python学习视频_Python学些路线):Day05 总结和练习
Python学习教程(Python学习视频_Python学些路线):总结和练习 练习清单 寻找"水仙花数". 寻找"完美数". "百钱百鸡" ...
- 『Python学习笔记』Python中的异步Web框架之fastAPI介绍RestAPI
Python中的异步Web框架之fastAPI介绍&RestAPI 文章目录 一. fastAPI简要介绍 1.1. 安装 1.2. 创建 1.3. get方法 1.4. post方法 1.5 ...
- Python学习教程(Python学习视频_Python学习路线):Day04循环结构
Python学习教程(Python学习视频_Python学习路线):循环结构 循环结构的应用场景 如果在程序中我们需要重复的执行某条或某些指令,例如用程序控制机器人踢足球,如果机器人持球而且还没有进入 ...
- python框架sanic_Python Web框架Sanic框架初识
Sanic 既是一个Python web 服务器,又是一个web框架,它为速度而生.Sanic只支持Python 3.5及其以上版本,允许我们使用 async/await 语法来使我们的代码非阻塞且快 ...
- python sanic_python高性能web框架Sanic学习--url
本文基于sanic 官方文档解释及自己阅读后的感想. 首先什么是sanic? sanic是一款用python3.5+写的web framework.它有一下几个特点: 1.flask-like的语法风 ...
最新文章
- VS2010在工具栏上创建查找组合框,即:CMFCToolBar中加入CMFCToolBarComboBoxButton
- MyBatis中获取对应日期的记录的mapper语句
- suse redhat 查看系统版本
- Python矩阵处理库—Numpy库的基本使用
- Silverlight 布局控件
- 转:Some interesting facts about SharePoint 2007 Search
- Python Pandas –操作
- “三峡水怪”的真面目竟是这个!水怪:我不要面子的吗?
- 【CodeSnippet】Gradle
- es5创建对象与继承
- java 权限url权限_Java秒杀系统实战系列~整合Shiro实现用户登录认证
- 容器化RDS|计算存储分离 or 本地存储?
- 三菱PLC通用快捷键
- Duplicate Net Names Wire Net......
- 使用ADB卸载Android内置应用
- burp小技巧之抓单个网站包
- linux 密码字典生成,Linux下的字典生成工具Crunch 创造自己的专属字典
- 802.1x EAP(证书)、PEAP(证书、EAP-MSCHAP v2)认证配置(NPS、组策略)
- 3d Max安装失败(Microsoft Visual C++ 2010 SP1 Redistributable (x86)	Failed...
- 如何下载 International Conference on Machine Learning(ICML)顶会的论文?从哪找ICML顶会论文?如何判断会议和期刊级别?