本文整理汇总了Python中sanic.response.file_stream方法的典型用法代码示例。如果您正苦于以下问题:Python response.file_stream方法的具体用法?Python response.file_stream怎么用?Python response.file_stream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.response
的用法示例。
在下文中一共展示了response.file_stream方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_server_app
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def model_server_app(model_path: Text, model_hash: Text = "somehash"):
app = Sanic(__name__)
app.number_of_model_requests = 0
@app.route("/model", methods=['GET'])
async def model(request):
"""Simple HTTP model server responding with a trained model."""
if model_hash == request.headers.get("If-None-Match"):
return response.text("", 204)
app.number_of_model_requests += 1
return await response.file_stream(
location=model_path,
headers={'ETag': model_hash,
'filename': model_path},
mime_type='application/zip')
return app
示例2: model_server_app
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def model_server_app(model_path: Text, model_hash: Text = "somehash") -> Sanic:
app = Sanic(__name__)
app.number_of_model_requests = 0
@app.route("/model", methods=["GET"])
async def model(request: Request) -> StreamingHTTPResponse:
"""Simple HTTP model server responding with a trained model."""
if model_hash == request.headers.get("If-None-Match"):
return response.text("", 204)
app.number_of_model_requests += 1
return await response.file_stream(
location=model_path,
headers={"ETag": model_hash, "filename": model_path},
mime_type="application/gzip",
)
return app
示例3: handler_file_stream
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def handler_file_stream(request):
return await response.file_stream(
Path("../") / "setup.py", chunk_size=1024
)
示例4: test_file_stream
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def test_file_stream(request):
return await response.file_stream(os.path.abspath("setup.py"),
chunk_size=1024)
# ----------------------------------------------- #
# Exceptions
# ----------------------------------------------- #
示例5: cb_index_handler
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def cb_index_handler(request, project_uuid=None, host=None):
return await response.file_stream(os.path.abspath('./public/index.html'))
示例6: cb_bundle_handler
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def cb_bundle_handler(request):
return await response.file_stream(os.path.abspath('./public/bundle.js'))
示例7: cb_serve_media_file
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def cb_serve_media_file(request, filename=""):
return await response.file_stream(os.path.join('./public', os.path.basename(request.path)))
示例8: _setup_blueprint_routes
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file_stream [as 别名]
def _setup_blueprint_routes(self, blueprint: Blueprint, config: Config) -> None:
"""Add routes to the application blueprint"""
@blueprint.websocket("/stream") # type: ignore
async def model_stream(
request: request.Request, socket: WebSocketCommonProtocol
) -> None:
async def sock_recv() -> LayoutEvent:
message = json.loads(await socket.recv())
event = message["body"]["event"]
return LayoutEvent(event["target"], event["data"])
async def sock_send(data: Dict[str, Any]) -> None:
message = {"header": {}, "body": {"render": data}}
await socket.send(json.dumps(message, separators=(",", ":")))
param_dict = {k: request.args.get(k) for k in request.args}
await self._run_renderer(sock_send, sock_recv, param_dict)
def handler_name(function: Any) -> str:
return f"{blueprint.name}.{function.__name__}"
if config["server_static_files"]:
@blueprint.route("/client/<path:path>") # type: ignore
async def client_files(
request: request.Request, path: str
) -> response.HTTPResponse:
file_extensions = [".html", ".js", ".json"]
abs_path = find_path(path)
return (
(await response.file_stream(str(abs_path)))
if abs_path is not None and abs_path.suffix in file_extensions
else response.text(f"Could not find: {path!r}", status=404)
)
if config["redirect_root_to_index"]:
@blueprint.route("/") # type: ignore
def redirect_to_index(request: request.Request) -> response.HTTPResponse:
return response.redirect(
request.app.url_for(handler_name(client_files), path="index.html")
)