From 1f5f2e169c1b875ed2c124b0e8a644408cbd3a4b Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 28 Apr 2017 18:04:42 -0400 Subject: [PATCH] directory listings Signed-off-by: Kenneth Reitz --- index.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/index.py b/index.py index cafe002..6a8040e 100644 --- a/index.py +++ b/index.py @@ -96,6 +96,12 @@ def find(endswith, dir, path): if '{0}{1}'.format(path, endswith) in fs_path: return fs_path +def directory_listing(path): + html = '' + for i in os.listdir(path): + html += '
  • {0}
  • '.format(i) + return html + def do_serve(dir, port): """Runs the 'serve' command, from the CLI.""" @@ -103,9 +109,11 @@ def do_serve(dir, port): dir = convert_dir(dir) port = convert_port(port) + os.chdir(dir) + app = Flask(__name__) - @app.route('/', defaults={'path': ''}) + @app.route('/', defaults={'path': './'}) @app.route('/') def catch_all(path): @@ -137,10 +145,18 @@ def do_serve(dir, port): return c.out elif '.html' in found: + # Strip prepending slashes. + if found.startswith('/'): + found = found[1:] + + # Open the file, and spit out the contents. with open(found) as html: return html.read() else: + if os.path.isdir(path): + return directory_listing(path) + abort(404)