Serving Static Sites

Serves plain static HTML/CSS/JS files to the user when a full framework is overkill. Use for a single landing page or quick prototype — never use python -m http.server, it exposes a directory listing.

aaravriyer193 Updated

File contents

Serving static sites

python -m http.server exposes a raw directory listing of the whole folder to anyone who opens the URL — looks broken and leaks more than intended. Use a two-line Flask app instead:

from flask import Flask, send_from_directory

app = Flask(__name__)

@app.route("/")
def index():
    return send_from_directory(".", "index.html")

@app.route("/<path:path>")
def static_file(path):
    return send_from_directory(".", path)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)
pip install flask

Run it backgrounded from the folder containing the site, then open_port on 8000:

shell: cd /home/user/site && python app.py    (background: true)
open_port: 8000

aaravriyer193/skills/tree/main/serving-static-sites commit 5ebdfcc717

Frequently asked questions

npx skillmds@latest add aaravriyer193/serving-static-sites