[Python-checkins] bpo-46285: Add command-line option -p/--protocol to module http.server (#30999)

JelleZijlstra webhook-mailer at python.org
Mon May 2 18:29:05 EDT 2022


https://github.com/python/cpython/commit/2d30adee72317e569d24739243cdd6f7631fa68f
commit: 2d30adee72317e569d24739243cdd6f7631fa68f
branch: main
author: Géry Ogam <gery.ogam at gmail.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-05-02T16:28:45-06:00
summary:

bpo-46285: Add command-line option -p/--protocol to module http.server (#30999)

Co-authored-by: Éric <merwok at netwok.org>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>

files:
A Misc/NEWS.d/next/Library/2022-01-29-00-23-00.bpo-46285.pt84qm.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 0de02834401aa..9d5e5e3a75b19 100644
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -157,7 +157,9 @@ provides three different variants:
 
    .. attribute:: protocol_version
 
-      This specifies the HTTP protocol version used in responses.  If set to
+      Specifies the HTTP version to which the server is conformant. It is sent
+      in responses to let the client know the server's communication
+      capabilities for future requests. If set to
       ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
       however, your server *must* then include an accurate ``Content-Length``
       header (using :meth:`send_header`) in all of its responses to clients.
@@ -193,7 +195,7 @@ provides three different variants:
 
    .. method:: handle_expect_100()
 
-      When a HTTP/1.1 compliant server receives an ``Expect: 100-continue``
+      When an HTTP/1.1 conformant server receives an ``Expect: 100-continue``
       request header it responds back with a ``100 Continue`` followed by ``200
       OK`` headers.
       This method can be overridden to raise an error if the server does not
@@ -444,6 +446,15 @@ the following command uses a specific directory::
 .. versionadded:: 3.7
     ``--directory`` argument was introduced.
 
+By default, the server is conformant to HTTP/1.0. The option ``-p/--protocol``
+specifies the HTTP version to which the server is conformant. For example, the
+following command runs an HTTP/1.1 conformant server::
+
+        python -m http.server --protocol HTTP/1.1
+
+.. versionadded:: 3.11
+    ``--protocol`` argument was introduced.
+
 .. class:: CGIHTTPRequestHandler(request, client_address, server)
 
    This class is used to serve either files or output of CGI scripts from the
diff --git a/Lib/http/server.py b/Lib/http/server.py
index a6100d4829751..d115dfc162bb1 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -1256,15 +1256,19 @@ def test(HandlerClass=BaseHTTPRequestHandler,
     parser = argparse.ArgumentParser()
     parser.add_argument('--cgi', action='store_true',
                         help='run as CGI server')
-    parser.add_argument('--bind', '-b', metavar='ADDRESS',
-                        help='specify alternate bind address '
+    parser.add_argument('-b', '--bind', metavar='ADDRESS',
+                        help='bind to this address '
                              '(default: all interfaces)')
-    parser.add_argument('--directory', '-d', default=os.getcwd(),
-                        help='specify alternate directory '
+    parser.add_argument('-d', '--directory', default=os.getcwd(),
+                        help='serve this directory '
                              '(default: current directory)')
-    parser.add_argument('port', action='store', default=8000, type=int,
-                        nargs='?',
-                        help='specify alternate port (default: 8000)')
+    parser.add_argument('-p', '--protocol', metavar='VERSION',
+                        default='HTTP/1.0',
+                        help='conform to this HTTP version '
+                             '(default: %(default)s)')
+    parser.add_argument('port', default=8000, type=int, nargs='?',
+                        help='bind to this port '
+                             '(default: %(default)s)')
     args = parser.parse_args()
     if args.cgi:
         handler_class = CGIHTTPRequestHandler
@@ -1290,4 +1294,5 @@ def finish_request(self, request, client_address):
         ServerClass=DualStackServer,
         port=args.port,
         bind=args.bind,
+        protocol=args.protocol,
     )
diff --git a/Misc/NEWS.d/next/Library/2022-01-29-00-23-00.bpo-46285.pt84qm.rst b/Misc/NEWS.d/next/Library/2022-01-29-00-23-00.bpo-46285.pt84qm.rst
new file mode 100644
index 0000000000000..83c4990e475d3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-29-00-23-00.bpo-46285.pt84qm.rst
@@ -0,0 +1,4 @@
+Add command-line option ``-p``/``--protocol`` to module :mod:`http.server`
+which specifies the HTTP version to which the server is conformant (HTTP/1.1
+conformant servers can now be run from the command-line interface of module
+:mod:`http.server`). Patch by Géry Ogam.



More information about the Python-checkins mailing list