Skip to content

Commit 2d30ade

Browse files
geryogammerwokJelleZijlstra
authored
bpo-46285: Add command-line option -p/--protocol to module http.server (#30999)
Co-authored-by: Éric <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 32e4f45 commit 2d30ade

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,15 +1256,19 @@ def test(HandlerClass=BaseHTTPRequestHandler,
12561256
parser = argparse.ArgumentParser()
12571257
parser.add_argument('--cgi', action='store_true',
12581258
help='run as CGI server')
1259-
parser.add_argument('--bind', '-b', metavar='ADDRESS',
1260-
help='specify alternate bind address '
1259+
parser.add_argument('-b', '--bind', metavar='ADDRESS',
1260+
help='bind to this address '
12611261
'(default: all interfaces)')
1262-
parser.add_argument('--directory', '-d', default=os.getcwd(),
1263-
help='specify alternate directory '
1262+
parser.add_argument('-d', '--directory', default=os.getcwd(),
1263+
help='serve this directory '
12641264
'(default: current directory)')
1265-
parser.add_argument('port', action='store', default=8000, type=int,
1266-
nargs='?',
1267-
help='specify alternate port (default: 8000)')
1265+
parser.add_argument('-p', '--protocol', metavar='VERSION',
1266+
default='HTTP/1.0',
1267+
help='conform to this HTTP version '
1268+
'(default: %(default)s)')
1269+
parser.add_argument('port', default=8000, type=int, nargs='?',
1270+
help='bind to this port '
1271+
'(default: %(default)s)')
12681272
args = parser.parse_args()
12691273
if args.cgi:
12701274
handler_class = CGIHTTPRequestHandler
@@ -1290,4 +1294,5 @@ def finish_request(self, request, client_address):
12901294
ServerClass=DualStackServer,
12911295
port=args.port,
12921296
bind=args.bind,
1297+
protocol=args.protocol,
12931298
)
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)