Skip to content

Commit a286caa

Browse files
authored
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.
1 parent 64ed609 commit a286caa

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

Doc/library/http.server.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ the current directory::
413413
print("serving at port", PORT)
414414
httpd.serve_forever()
415415

416+
417+
:class:`SimpleHTTPRequestHandler` can also be subclassed to enhance behavior,
418+
such as using different index file names by overriding the class attribute
419+
:attr:`index_pages`.
420+
416421
.. _http-server-cli:
417422

418423
:mod:`http.server` can also be invoked directly using the :option:`-m`

Lib/http/server.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,20 +652,18 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
652652
653653
"""
654654

655-
index_pages = ["index.html", "index.htm"]
656655
server_version = "SimpleHTTP/" + __version__
656+
index_pages = ("index.html", "index.htm")
657657
extensions_map = _encodings_map_default = {
658658
'.gz': 'application/gzip',
659659
'.Z': 'application/octet-stream',
660660
'.bz2': 'application/x-bzip2',
661661
'.xz': 'application/x-xz',
662662
}
663663

664-
def __init__(self, *args, directory=None, index_pages=None, **kwargs):
664+
def __init__(self, *args, directory=None, **kwargs):
665665
if directory is None:
666666
directory = os.getcwd()
667-
if index_pages is not None:
668-
self.index_pages = index_pages
669667
self.directory = os.fspath(directory)
670668
super().__init__(*args, **kwargs)
671669

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change ``SimpleHTTPRequestHandler`` to support subclassing to provide a
2+
different set of index file names instead of using ``__init__`` parameters.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy