Flaskで静的ファイルを提供する方法 質問する

Flaskで静的ファイルを提供する方法 質問する

恥ずかしい話です。私は、急ごしらえで作ったアプリケーションを持っていますFlaskが、今のところ、CSS と JS へのリンクがいくつかある静的 HTML ページ 1 つを提供しているだけです。また、ドキュメントのどこにFlask静的ファイルを返すかが説明されているのかわかりません。確かに、使用することはできますが、データがテンプレート化されていないことはわかっています。またはが正しいrender_templateと考えていたのですが、うまく動作しませんでした。その間、ファイルを開いてコンテンツを読み取り、適切な mimetype で を設定しています。send_fileurl_forResponse

import os.path

from flask import Flask, Response


app = Flask(__name__)
app.config.from_object(__name__)


def root_dir():  # pragma: no cover
    return os.path.abspath(os.path.dirname(__file__))


def get_file(filename):  # pragma: no cover
    try:
        src = os.path.join(root_dir(), filename)
        # Figure out how flask returns static files
        # Tried:
        # - render_template
        # - send_file
        # This should not be so non-obvious
        return open(src).read()
    except IOError as exc:
        return str(exc)


@app.route('/', methods=['GET'])
def metrics():  # pragma: no cover
    content = get_file('jenkins_analytics.html')
    return Response(content, mimetype="text/html")


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def get_resource(path):  # pragma: no cover
    mimetypes = {
        ".css": "text/css",
        ".html": "text/html",
        ".js": "application/javascript",
    }
    complete_path = os.path.join(root_dir(), path)
    ext = os.path.splitext(path)[1]
    mimetype = mimetypes.get(ext, "text/html")
    content = get_file(complete_path)
    return Response(content, mimetype=mimetype)


if __name__ == '__main__':  # pragma: no cover
    app.run(port=80)

誰かこれのコードサンプルか URL を教えてもらえませんか? これは非常に簡単なことだと思います。

ベストアンサー1

運用環境では、静的フォルダーからのリクエストを処理するために、アプリケーションの前面に HTTP サーバー (Nginx、Apache など) を構成します/static。専用の Web サーバーは静的ファイルを効率的に処理するのに非常に優れていますが、低ボリュームでは Flask と比べて違いがわからない可能性があります。

Flask は、Flask アプリを定義する Python モジュールの隣のフォルダーの下に/static/<path:filename>ある任意のルートを自動的に作成します。静的ファイルにリンクするには、次を使用します。filenamestaticurl_forurl_for('static', filename='js/analytics.js')

使用することもできますsend_from_directory独自のルートでディレクトリからファイルを提供します。これはベース ディレクトリとパスを受け取り、パスがディレクトリに含まれていることを確認します。これにより、ユーザーが提供するパスを安全に受け入れることができます。これは、ログインしたユーザーに権限があるかどうかなど、ファイルを提供する前に何かを確認したい場合に便利です。

from flask import send_from_directory

@app.route('/reports/<path:path>')
def send_report(path):
    return send_from_directory('reports', path)

またはをユーザー指定のパスと一緒に使用しないでください。これにより、send_filesend_static_fileディレクトリトラバーサル攻撃. は、send_from_directory既知のディレクトリ下のユーザー指定のパスを安全に処理するように設計されており、パスがディレクトリを脱出しようとするとエラーが発生します。

ファイルシステムに書き込まずにメモリ内にファイルを生成する場合は、BytesIOオブジェクトを渡すことでsend_fileファイルのように提供します。send_fileこの場合、ファイル名やコンテンツ タイプなどを推測できないため、他の引数を渡す必要があります。

おすすめ記事