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)