Skip to content

Commit 2e58e23

Browse files
committed
Add command-line option -p/--protocol to module http.server
1 parent ef723f4 commit 2e58e23

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

Doc/library/http.server.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ provides three different variants:
157157

158158
.. attribute:: protocol_version
159159

160-
This specifies the HTTP protocol version used in responses. If set to
160+
Specifies the HTTP version to which the server is conformant. It is sent
161+
in responses to let the client know the server's communication
162+
capabilities for future requests. If set to
161163
``'HTTP/1.1'``, the server will permit HTTP persistent connections;
162164
however, your server *must* then include an accurate ``Content-Length``
163165
header (using :meth:`send_header`) in all of its responses to clients.
@@ -193,7 +195,7 @@ provides three different variants:
193195

194196
.. method:: handle_expect_100()
195197

196-
When a HTTP/1.1 compliant server receives an ``Expect: 100-continue``
198+
When an HTTP/1.1 conformant server receives an ``Expect: 100-continue``
197199
request header it responds back with a ``100 Continue`` followed by ``200
198200
OK`` headers.
199201
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::
444446
.. versionadded:: 3.7
445447
``--directory`` argument was introduced.
446448

449+
By default, the server is conformant to HTTP/1.0. The option ``-p/--protocol``
450+
specifies the HTTP version to which the server is conformant. For example, the
451+
following command runs an HTTP/1.1 conformant server::
452+
453+
python -m http.server --protocol HTTP/1.1
454+
455+
.. versionadded:: 3.11
456+
``--protocol`` argument was introduced.
457+
447458
.. class:: CGIHTTPRequestHandler(request, client_address, server)
448459

449460
This class is used to serve either files or output of CGI scripts from the

Lib/http/server.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,15 +1258,19 @@ def test(HandlerClass=BaseHTTPRequestHandler,
12581258
parser = argparse.ArgumentParser()
12591259
parser.add_argument('--cgi', action='store_true',
12601260
help='run as CGI server')
1261-
parser.add_argument('--bind', '-b', metavar='ADDRESS',
1262-
help='specify alternate bind address '
1261+
parser.add_argument('-b', '--bind', metavar='ADDRESS',
1262+
help='specify the address to bind to '
12631263
'(default: all interfaces)')
1264-
parser.add_argument('--directory', '-d', default=os.getcwd(),
1265-
help='specify alternate directory '
1264+
parser.add_argument('-d', '--directory', default=os.getcwd(),
1265+
help='specify the directory to serve '
12661266
'(default: current directory)')
1267+
parser.add_argument('-p', '--protocol', default='HTTP/1.0',
1268+
help='specify the HTTP version to conform to '
1269+
'(default: %(default)s)')
12671270
parser.add_argument('port', action='store', default=8000, type=int,
12681271
nargs='?',
1269-
help='specify alternate port (default: 8000)')
1272+
help='specify the port to bind to '
1273+
'(default: %(default)s)')
12701274
args = parser.parse_args()
12711275
if args.cgi:
12721276
handler_class = CGIHTTPRequestHandler
@@ -1292,4 +1296,5 @@ def finish_request(self, request, client_address):
12921296
ServerClass=DualStackServer,
12931297
port=args.port,
12941298
bind=args.bind,
1299+
protocol=args.protocol,
12951300
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add command-line option ``-p``/``--protocol`` to module :mod:`http.server`
2+
which specifies the HTTP version to which the server is conformant (HTTP/1.1
3+
conformant servers can now be run from the command-line interface of module
4+
:mod:`http.server`). Patch by Géry Ogam.

0 commit comments

Comments
 (0)