diff options
author | Géry Ogam <gery.ogam@gmail.com> | 2022-02-03 15:51:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-03 15:51:05 (GMT) |
commit | 2d080347d74078a55c47715d232d1ab8dc8cd603 (patch) | |
tree | aec3ac0d2b90f3ca5cd3e4839c939e2289a47706 /Lib/http | |
parent | 734b1f119be6f0dcd6845c78a9e0a71d88a90b59 (diff) | |
download | cpython-2d080347d74078a55c47715d232d1ab8dc8cd603.zip cpython-2d080347d74078a55c47715d232d1ab8dc8cd603.tar.gz cpython-2d080347d74078a55c47715d232d1ab8dc8cd603.tar.bz2 |
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
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/server.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py index 4f9b8a1..194a503 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -103,8 +103,6 @@ import socketserver import sys import time import urllib.parse -import contextlib -from functools import partial from http import HTTPStatus @@ -1239,7 +1237,6 @@ def test(HandlerClass=BaseHTTPRequestHandler, """ ServerClass.address_family, addr = _get_best_family(bind, port) - HandlerClass.protocol_version = protocol with ServerClass(addr, HandlerClass) as httpd: host, port = httpd.socket.getsockname()[:2] @@ -1256,29 +1253,29 @@ def test(HandlerClass=BaseHTTPRequestHandler, if __name__ == '__main__': import argparse + import contextlib parser = argparse.ArgumentParser() parser.add_argument('--cgi', action='store_true', - help='Run as CGI Server') + help='run as CGI server') parser.add_argument('--bind', '-b', metavar='ADDRESS', - help='Specify alternate bind address ' - '[default: all interfaces]') + help='specify alternate bind address ' + '(default: all interfaces)') parser.add_argument('--directory', '-d', default=os.getcwd(), - help='Specify alternative directory ' - '[default:current directory]') - parser.add_argument('port', action='store', - default=8000, type=int, + help='specify alternate directory ' + '(default: current directory)') + parser.add_argument('port', action='store', default=8000, type=int, nargs='?', - help='Specify alternate port [default: 8000]') + help='specify alternate port (default: 8000)') args = parser.parse_args() if args.cgi: handler_class = CGIHTTPRequestHandler else: - handler_class = partial(SimpleHTTPRequestHandler, - directory=args.directory) + handler_class = SimpleHTTPRequestHandler # ensure dual-stack is not disabled; ref #38907 class DualStackServer(ThreadingHTTPServer): + def server_bind(self): # suppress exception when protocol is IPv4 with contextlib.suppress(Exception): @@ -1286,6 +1283,10 @@ if __name__ == '__main__': socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) return super().server_bind() + def finish_request(self, request, client_address): + self.RequestHandlerClass(request, client_address, self, + directory=args.directory) + test( HandlerClass=handler_class, ServerClass=DualStackServer, |