本文整理匯總了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")
)