Skip to content

Commit 502ad39

Browse files
bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701)
Fix command-line option -d/--directory in http.server main function that was ignored when combined with --cgi. Automerge-Triggered-By: GH:merwok (cherry picked from commit 2d08034) Co-authored-by: Géry Ogam <[email protected]> Co-authored-by: Géry Ogam <[email protected]>
1 parent 1953f03 commit 502ad39

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Lib/http/server.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@
103103
import sys
104104
import time
105105
import urllib.parse
106-
import contextlib
107-
from functools import partial
108106

109107
from http import HTTPStatus
110108

@@ -1240,7 +1238,6 @@ def test(HandlerClass=BaseHTTPRequestHandler,
12401238
12411239
"""
12421240
ServerClass.address_family, addr = _get_best_family(bind, port)
1243-
12441241
HandlerClass.protocol_version = protocol
12451242
with ServerClass(addr, HandlerClass) as httpd:
12461243
host, port = httpd.socket.getsockname()[:2]
@@ -1257,36 +1254,40 @@ def test(HandlerClass=BaseHTTPRequestHandler,
12571254

12581255
if __name__ == '__main__':
12591256
import argparse
1257+
import contextlib
12601258

12611259
parser = argparse.ArgumentParser()
12621260
parser.add_argument('--cgi', action='store_true',
1263-
help='Run as CGI Server')
1261+
help='run as CGI server')
12641262
parser.add_argument('--bind', '-b', metavar='ADDRESS',
1265-
help='Specify alternate bind address '
1266-
'[default: all interfaces]')
1263+
help='specify alternate bind address '
1264+
'(default: all interfaces)')
12671265
parser.add_argument('--directory', '-d', default=os.getcwd(),
1268-
help='Specify alternative directory '
1269-
'[default:current directory]')
1270-
parser.add_argument('port', action='store',
1271-
default=8000, type=int,
1266+
help='specify alternate directory '
1267+
'(default: current directory)')
1268+
parser.add_argument('port', action='store', default=8000, type=int,
12721269
nargs='?',
1273-
help='Specify alternate port [default: 8000]')
1270+
help='specify alternate port (default: 8000)')
12741271
args = parser.parse_args()
12751272
if args.cgi:
12761273
handler_class = CGIHTTPRequestHandler
12771274
else:
1278-
handler_class = partial(SimpleHTTPRequestHandler,
1279-
directory=args.directory)
1275+
handler_class = SimpleHTTPRequestHandler
12801276

12811277
# ensure dual-stack is not disabled; ref #38907
12821278
class DualStackServer(ThreadingHTTPServer):
1279+
12831280
def server_bind(self):
12841281
# suppress exception when protocol is IPv4
12851282
with contextlib.suppress(Exception):
12861283
self.socket.setsockopt(
12871284
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
12881285
return super().server_bind()
12891286

1287+
def finish_request(self, request, client_address):
1288+
self.RequestHandlerClass(request, client_address, self,
1289+
directory=args.directory)
1290+
12901291
test(
12911292
HandlerClass=handler_class,
12921293
ServerClass=DualStackServer,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix command-line option ``-d``/``--directory`` in module :mod:`http.server`
2+
which is ignored when combined with command-line option ``--cgi``. Patch by
3+
Géry Ogam.

0 commit comments

Comments
 (0)