@@ -39,7 +39,17 @@ handler. Code to create and run the server looks like this::
3939 This class builds on the :class: `~socketserver.TCPServer ` class by storing
4040 the server address as instance variables named :attr: `server_name ` and
4141 :attr: `server_port `. The server is accessible by the handler, typically
42- through the handler's :attr: `server ` instance variable.
42+ through the handler's :attr: `~socketserver.BaseRequestHandler.server `
43+ instance variable.
44+
45+ .. attribute :: server_name
46+
47+ The HTTP server's fully qualified domain name.
48+
49+ .. attribute :: server_port
50+
51+ The HTTP server's port number obtained from *server_address *.
52+
4353
4454.. class :: ThreadingHTTPServer(server_address, RequestHandlerClass)
4555
@@ -60,7 +70,7 @@ handler. Code to create and run the server looks like this::
6070 object fails with a :exc: `RuntimeError `.
6171
6272 The *certfile * argument is the path to the SSL certificate chain file,
63- and the *keyfile * is the path to file containing the private key.
73+ and the *keyfile * is the path to the file containing the private key.
6474
6575 A *password * can be specified for files protected and wrapped with PKCS#8,
6676 but beware that this could possibly expose hardcoded passwords in clear.
@@ -140,7 +150,7 @@ instantiation, of which this module provides three different variants:
140150
141151 .. attribute :: path
142152
143- Contains the request path. If query component of the URL is present,
153+ Contains the request path. If the query component of the URL is present,
144154 then ``path `` includes the query. Using the terminology of :rfc: `3986 `,
145155 ``path `` here includes ``hier-part `` and the ``query ``.
146156
@@ -190,7 +200,7 @@ instantiation, of which this module provides three different variants:
190200 Specifies a format string that should be used by :meth: `send_error ` method
191201 for building an error response to the client. The string is filled by
192202 default with variables from :attr: `responses ` based on the status code
193- that passed to :meth: `send_error `.
203+ passed to :meth: `send_error `.
194204
195205 .. attribute :: error_content_type
196206
@@ -238,8 +248,8 @@ instantiation, of which this module provides three different variants:
238248 .. method :: handle_expect_100()
239249
240250 When an HTTP/1.1 conformant server receives an ``Expect: 100-continue ``
241- request header it responds back with a ``100 Continue `` followed by ``200
242- OK `` headers.
251+ request header it responds with a ``100 Continue `` followed by ``200 OK ``
252+ headers.
243253 This method can be overridden to raise an error if the server does not
244254 want the client to continue. For example, the server can choose to send ``417
245255 Expectation Failed `` as a response header and ``return False ``.
@@ -295,8 +305,8 @@ instantiation, of which this module provides three different variants:
295305 .. method :: send_response_only(code, message=None)
296306
297307 Sends the response header only, used for the purposes when ``100
298- Continue `` response is sent by the server to the client. The headers not
299- buffered and sent directly the output stream.If the *message * is not
308+ Continue `` response is sent by the server to the client. The headers are
309+ not buffered and sent directly the output stream. If the *message * is not
300310 specified, the HTTP message corresponding the response *code * is sent.
301311
302312 This method does not reject *message * containing CRLF sequences.
@@ -338,7 +348,7 @@ instantiation, of which this module provides three different variants:
338348 to create custom error logging mechanisms. The *format * argument is a
339349 standard printf-style format string, where the additional arguments to
340350 :meth: `log_message ` are applied as inputs to the formatting. The client
341- ip address and current date and time are prefixed to every message logged.
351+ IP address and current date and time are prefixed to every message logged.
342352
343353 .. method :: version_string()
344354
@@ -390,6 +400,14 @@ instantiation, of which this module provides three different variants:
390400 This will be ``"SimpleHTTP/" + __version__ ``, where ``__version__ `` is
391401 defined at the module level.
392402
403+ .. attribute :: index_pages
404+
405+ Specifies the filenames that are treated as directory index pages.
406+
407+ Defaults to ``("index.html", "index.htm") ``.
408+
409+ .. versionadded :: 3.12
410+
393411 .. attribute :: extensions_map
394412
395413 A dictionary mapping suffixes into MIME types, contains custom overrides
@@ -413,8 +431,8 @@ instantiation, of which this module provides three different variants:
413431 The request is mapped to a local file by interpreting the request as a
414432 path relative to the current working directory.
415433
416- If the request was mapped to a directory, the directory is checked for a
417- file named `` index.html `` or `` index.htm `` (in that order) . If found, the
434+ If the request was mapped to a directory, the directory is checked for
435+ an index page as specified by :attr: ` index_pages ` . If found, the
418436 file's contents are returned; otherwise a directory listing is generated
419437 by calling the :meth: `list_directory ` method. This method uses
420438 :func: `os.listdir ` to scan the directory, and returns a ``404 `` error
@@ -441,9 +459,32 @@ instantiation, of which this module provides three different variants:
441459 .. versionchanged :: 3.7
442460 Support of the ``'If-Modified-Since' `` header.
443461
444- The :class: `SimpleHTTPRequestHandler ` class can be used in the following
445- manner in order to create a very basic webserver serving files relative to
446- the current directory::
462+ .. method :: list_directory(path)
463+
464+ Helper to list the contents of *path * when no index page is present.
465+
466+ This returns either a :term: `file-like object ` (which must be closed
467+ by the caller) or ``None `` to indicate an error, in which case the
468+ caller has nothing further to do. In either case, the headers are sent.
469+
470+ .. method :: guess_type(path)
471+
472+ Guess the type of the file at the given *path *.
473+
474+ This returns a string of the form ``type/subtype ``, usable for
475+ a MIME Content-type header.
476+
477+ The default implementation looks the file's extension up in
478+ :attr: `extensions_map `, falling back to
479+ :func: `mimetypes.guess_file_type ` and then to
480+ ``'application/octet-stream' ``.
481+
482+ .. versionchanged :: 3.13
483+ Add :func: `mimetypes.guess_file_type ` as a fallback.
484+
485+
486+ The :class: `SimpleHTTPRequestHandler ` class can be used to create a very basic
487+ webserver serving files relative to the current directory as follows::
447488
448489 import http.server
449490 import socketserver
@@ -459,7 +500,7 @@ the current directory::
459500
460501:class: `SimpleHTTPRequestHandler ` can also be subclassed to enhance behavior,
461502such as using different index file names by overriding the class attribute
462- :attr: `index_pages `.
503+ :attr: `~SimpleHTTPRequestHandler. index_pages `.
463504
464505
465506.. class :: CGIHTTPRequestHandler(request, client_address, server)
@@ -480,7 +521,9 @@ such as using different index file names by overriding the class attribute
480521 the other common server configuration is to treat special extensions as
481522 denoting CGI scripts.
482523
483- The :func: `do_GET ` and :func: `do_HEAD ` functions are modified to run CGI scripts
524+ The :func: `~SimpleHTTPRequestHandler.do_GET ` and
525+ :func: `~SimpleHTTPRequestHandler.do_HEAD ` functions
526+ are modified to run CGI scripts
484527 and serve the output, instead of serving files, if the request leads to
485528 somewhere below the ``cgi_directories `` path.
486529
@@ -637,13 +680,13 @@ Security considerations
637680.. index :: pair: http.server; security
638681
639682:class: `SimpleHTTPRequestHandler ` will follow symbolic links when handling
640- requests, this makes it possible for files outside of the specified directory
683+ requests which makes it possible for files outside of the specified directory
641684to be served.
642685
643686Methods :meth: `BaseHTTPRequestHandler.send_header ` and
644687:meth: `BaseHTTPRequestHandler.send_response_only ` assume sanitized input
645688and do not perform input validation such as checking for the presence of CRLF
646- sequences. Untrusted input may result in HTTP Header injection attacks.
689+ sequences. Untrusted input may result in HTTP header injection attacks.
647690
648691Earlier versions of Python did not scrub control characters from the
649692log messages emitted to stderr from ``python -m http.server `` or the
0 commit comments