Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -899,14 +899,23 @@ def list_directory(self, path):
r.append(f'<title>{title}</title>\n</head>')
r.append(f'<body>\n<h1>{title}</h1>')
r.append('<hr>\n<ul>')
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
for entry in entries:
displayname = linkname = name = entry.name
# Ignore any OSError raised by the os.DirEntry methods
# to match the behavior of their os.path.* counterpart.
try:
is_dir = entry.is_dir()
except OSError:
is_dir = False
try:
is_symlink = entry.is_symlink()
except OSError:
is_symlink = False
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
if is_dir:
Comment thread
picnixz marked this conversation as resolved.
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
if is_symlink:
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
r.append('<li><a href="%s">%s</a></li>'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:class:`http.server.SimpleHTTPRequestHandler` now relies on :func:`os.scandir`
instead of :func:`os.listdir` to build directory listings, thereby reducing
the total number of :func:`os.stat` calls per entry. This typically improves
directory listing performance on filesystems where such calls are slow.
Loading