1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】Flaskで好きなHTTPヘッダーを付けて/favicon.icoを送信

Last updated at Posted at 2025-07-15
app.py
import os
from flask import Flask, send_from_directory, stream_template
from werkzeug.serving import WSGIRequestHandler


app = Flask(__name__)


@app.after_request
def add_headers(response):
    # サーバ名を変更
    WSGIRequestHandler.server_version = 'Apache'
    WSGIRequestHandler.sys_version = ''
    # Etag削除
    del response.headers['Etag']
    # Dateが2つ現れるのを片方削除
    del response.headers['Date']
    # 自動で付与される情報を削除
    del response.headers['Content-Disposition']
    # 下記に入れたいヘッダーを追加する(Transfer-Encoding: chunkedを除く)
    response.headers['X-Frame-Options'] = 'DENY'
    response.headers['Cache-Control'] = 'no-cache, no-store'
    response.headers['Pragma'] = 'no-cache'
    if 'text/html' in response.headers.get('Content-Type'):
        response.headers['Content-Language'] = 'ja'
    elif 'image/x-icon' in response.headers.get('Content-Type'):
        response.headers['Vary'] = 'Origin,Access-Control-Request-Method,Access-Control-Request-Headers'
        response.headers['Accept-Ranges'] = 'bytes'
    return response


@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico')


@app.route('/')
def index():
    # Transfer-Encoding: chunkedを付ける
    return stream_template('index.html')


app.run(host='127.0.0.1', port=8080, threaded=True, debug=True)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?