From 0606d02831661df98d526d47567d0d63ed533761 Mon Sep 17 00:00:00 2001 From: Michael Bommarito Date: Sat, 20 Jun 2026 09:00:37 -0400 Subject: [PATCH 1/5] gh-151788: Speed up http.server directory listing by using os.scandir SimpleHTTPRequestHandler.list_directory() called os.path.isdir() and os.path.islink() for every entry, issuing two stat-family syscalls per file. This is wasted work on any filesystem and dominates listing time for large directories; on network filesystems such as NFS, where each call is a round-trip, it becomes severe. Use os.scandir(), whose DirEntry objects report the type from the directory read itself (d_type / READDIRPLUS), eliminating the per-entry stats in the common case and never doing more work than before. Co-Authored-By: Claude Opus 4.8 (1M context) --- Lib/http/server.py | 16 +++++++++------- ...026-06-20-09-00-26.gh-issue-151788.Gr8IxK.rst | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-20-09-00-26.gh-issue-151788.Gr8IxK.rst diff --git a/Lib/http/server.py b/Lib/http/server.py index ebc85052aecb90..915bec95655e76 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -872,13 +872,13 @@ def list_directory(self, path): """ try: - list = os.listdir(path) + with os.scandir(path) as it: + entries = sorted(it, key=lambda e: e.name.lower()) except OSError: self.send_error( HTTPStatus.NOT_FOUND, "No permission to list directory") return None - list.sort(key=lambda a: a.lower()) r = [] displaypath = self.path displaypath = displaypath.split('#', 1)[0] @@ -899,14 +899,16 @@ def list_directory(self, path): r.append(f'{title}\n') r.append(f'\n

{title}

') r.append('
\n