[Python-checkins] gh-91219: http - use subclassing to override index_pages attribute (GH-100731)

ethanfurman webhook-mailer at python.org
Tue Jan 3 18:20:14 EST 2023


https://github.com/python/cpython/commit/a286caa937405f7415dcc095a7ad5097c4433246
commit: a286caa937405f7415dcc095a7ad5097c4433246
branch: main
author: Ethan Furman <ethan at stoneleaf.us>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2023-01-03T15:20:08-08:00
summary:

gh-91219: http - use subclassing to override index_pages attribute (GH-100731)

Remove previously added parameter to `__init__`, and recommend subclassing to modify the `index_pages` attribute instead.

files:
A Misc/NEWS.d/next/Library/2023-01-03-11-06-28.gh-issue-91219.s5IFCw.rst
M Doc/library/http.server.rst
M Lib/http/server.py

diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
index 3290b9beab3e..ae75e6dc5fdc 100644
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -413,6 +413,11 @@ the current directory::
        print("serving at port", PORT)
        httpd.serve_forever()
 
+
+:class:`SimpleHTTPRequestHandler` can also be subclassed to enhance behavior,
+such as using different index file names by overriding the class attribute
+:attr:`index_pages`.
+
 .. _http-server-cli:
 
 :mod:`http.server` can also be invoked directly using the :option:`-m`
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 221c8be4ae4b..971f08046d50 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -652,8 +652,8 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
 
     """
 
-    index_pages = ["index.html", "index.htm"]
     server_version = "SimpleHTTP/" + __version__
+    index_pages = ("index.html", "index.htm")
     extensions_map = _encodings_map_default = {
         '.gz': 'application/gzip',
         '.Z': 'application/octet-stream',
@@ -661,11 +661,9 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         '.xz': 'application/x-xz',
     }
 
-    def __init__(self, *args, directory=None, index_pages=None, **kwargs):
+    def __init__(self, *args, directory=None, **kwargs):
         if directory is None:
             directory = os.getcwd()
-        if index_pages is not None:
-            self.index_pages = index_pages
         self.directory = os.fspath(directory)
         super().__init__(*args, **kwargs)
 
diff --git a/Misc/NEWS.d/next/Library/2023-01-03-11-06-28.gh-issue-91219.s5IFCw.rst b/Misc/NEWS.d/next/Library/2023-01-03-11-06-28.gh-issue-91219.s5IFCw.rst
new file mode 100644
index 000000000000..cb5e2e4630cc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-01-03-11-06-28.gh-issue-91219.s5IFCw.rst
@@ -0,0 +1,2 @@
+Change ``SimpleHTTPRequestHandler`` to support subclassing to provide a
+different set of index file names instead of using ``__init__`` parameters.



More information about the Python-checkins mailing list