Skip to content

Instantly share code, notes, and snippets.

@SuperZombi
Last active October 4, 2024 10:24
Show Gist options
  • Save SuperZombi/f5ee64d20f6c8374ba7b60eb17813020 to your computer and use it in GitHub Desktop.
Save SuperZombi/f5ee64d20f6c8374ba7b60eb17813020 to your computer and use it in GitHub Desktop.
Flask fast server
import os
from flask import Flask, send_from_directory, redirect, abort
app = Flask(__name__)
@app.route('/')
@app.route('/<path:filepath>')
def data(filepath="index"):
p = os.path.join("data", filepath)
if os.path.exists(p):
if os.path.isfile(p):
return send_from_directory('data', filepath)
if filepath[-1] != "/":
return redirect("/" + filepath + "/")
if os.path.isfile(os.path.join(p, "index.html")):
return send_from_directory('data', os.path.join(filepath, "index.html"))
if os.path.isfile(p + ".html"):
return send_from_directory('data', filepath + ".html")
if os.path.basename(filepath).endswith(".html"):
return redirect("/" + filepath.removesuffix('.html'))
if os.path.basename(filepath) == "index":
return redirect("/" + os.path.dirname(filepath) + "/")
abort(404)
if __name__ == '__main__':
app.run(host='0.0.0.0', port='80')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment