Skip to content

Commit 9a95fa9

Browse files
authored
gh-91219: Add an index_pages default list and parameter to SimpleHTTPRequestHandler (GH-31985)
* Add an index_pages default list to SimpleHTTPRequestHandler and an optional constructor parameter that allows the default indexes pages list to be overridden. This makes it easy to set a new index page name without having to override send_head.
1 parent 4e796f5 commit 9a95fa9

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Lib/http/server.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
642642
643643
"""
644644

645+
index_pages = ["index.html", "index.htm"]
645646
server_version = "SimpleHTTP/" + __version__
646647
extensions_map = _encodings_map_default = {
647648
'.gz': 'application/gzip',
@@ -650,9 +651,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
650651
'.xz': 'application/x-xz',
651652
}
652653

653-
def __init__(self, *args, directory=None, **kwargs):
654+
def __init__(self, *args, directory=None, index_pages=None, **kwargs):
654655
if directory is None:
655656
directory = os.getcwd()
657+
if index_pages is not None:
658+
self.index_pages = index_pages
656659
self.directory = os.fspath(directory)
657660
super().__init__(*args, **kwargs)
658661

@@ -696,7 +699,7 @@ def send_head(self):
696699
self.send_header("Content-Length", "0")
697700
self.end_headers()
698701
return None
699-
for index in "index.html", "index.htm":
702+
for index in self.index_pages:
700703
index = os.path.join(path, index)
701704
if os.path.exists(index):
702705
path = index
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add an index_pages parameter to support using non-default index page names.

0 commit comments

Comments
 (0)