add gzip
This commit is contained in:
@@ -1,12 +1,37 @@
|
|||||||
from flask import Flask, Response
|
from flask import Flask, Response, request
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
with open("bomb.png.br", "rb") as f:
|
||||||
|
BOMB_BR = f.read()
|
||||||
|
|
||||||
|
with open("bomb.png.gz", "rb") as f:
|
||||||
|
BOMB_GZIP = f.read()
|
||||||
|
|
||||||
|
print(f"[startup] br={len(BOMB_BR):,} bytes gzip={len(BOMB_GZIP):,} bytes")
|
||||||
|
|
||||||
|
|
||||||
|
def _pick_encoding():
|
||||||
|
accept = request.headers.get("Accept-Encoding", "")
|
||||||
|
if "br" in accept:
|
||||||
|
return "br", BOMB_BR
|
||||||
|
if "gzip" in accept:
|
||||||
|
return "gzip", BOMB_GZIP
|
||||||
|
return "gzip", BOMB_GZIP
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
data = open("bomb.png.br", "rb").read()
|
encoding, body = _pick_encoding()
|
||||||
return Response(data, mimetype="image/png", headers={"Content-Encoding": "br"})
|
return Response(
|
||||||
|
body,
|
||||||
|
mimetype="image/png",
|
||||||
|
headers={
|
||||||
|
"Content-Encoding": encoding,
|
||||||
|
"Content-Length": str(len(body)),
|
||||||
|
"Vary": "Accept-Encoding",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8080)
|
app.run(host="0.0.0.0", port=8393, threaded=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user