mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
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
This commit is contained in:
parent
734b1f119b
commit
2d080347d7
2 changed files with 17 additions and 13 deletions
|
@ -103,8 +103,6 @@ import socketserver
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import contextlib
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
|
@ -1239,7 +1237,6 @@ def test(HandlerClass=BaseHTTPRequestHandler,
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ServerClass.address_family, addr = _get_best_family(bind, port)
|
ServerClass.address_family, addr = _get_best_family(bind, port)
|
||||||
|
|
||||||
HandlerClass.protocol_version = protocol
|
HandlerClass.protocol_version = protocol
|
||||||
with ServerClass(addr, HandlerClass) as httpd:
|
with ServerClass(addr, HandlerClass) as httpd:
|
||||||
host, port = httpd.socket.getsockname()[:2]
|
host, port = httpd.socket.getsockname()[:2]
|
||||||
|
@ -1256,29 +1253,29 @@ def test(HandlerClass=BaseHTTPRequestHandler,
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import argparse
|
import argparse
|
||||||
|
import contextlib
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--cgi', action='store_true',
|
parser.add_argument('--cgi', action='store_true',
|
||||||
help='Run as CGI Server')
|
help='run as CGI server')
|
||||||
parser.add_argument('--bind', '-b', metavar='ADDRESS',
|
parser.add_argument('--bind', '-b', metavar='ADDRESS',
|
||||||
help='Specify alternate bind address '
|
help='specify alternate bind address '
|
||||||
'[default: all interfaces]')
|
'(default: all interfaces)')
|
||||||
parser.add_argument('--directory', '-d', default=os.getcwd(),
|
parser.add_argument('--directory', '-d', default=os.getcwd(),
|
||||||
help='Specify alternative directory '
|
help='specify alternate directory '
|
||||||
'[default:current directory]')
|
'(default: current directory)')
|
||||||
parser.add_argument('port', action='store',
|
parser.add_argument('port', action='store', default=8000, type=int,
|
||||||
default=8000, type=int,
|
|
||||||
nargs='?',
|
nargs='?',
|
||||||
help='Specify alternate port [default: 8000]')
|
help='specify alternate port (default: 8000)')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.cgi:
|
if args.cgi:
|
||||||
handler_class = CGIHTTPRequestHandler
|
handler_class = CGIHTTPRequestHandler
|
||||||
else:
|
else:
|
||||||
handler_class = partial(SimpleHTTPRequestHandler,
|
handler_class = SimpleHTTPRequestHandler
|
||||||
directory=args.directory)
|
|
||||||
|
|
||||||
# ensure dual-stack is not disabled; ref #38907
|
# ensure dual-stack is not disabled; ref #38907
|
||||||
class DualStackServer(ThreadingHTTPServer):
|
class DualStackServer(ThreadingHTTPServer):
|
||||||
|
|
||||||
def server_bind(self):
|
def server_bind(self):
|
||||||
# suppress exception when protocol is IPv4
|
# suppress exception when protocol is IPv4
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
|
@ -1286,6 +1283,10 @@ if __name__ == '__main__':
|
||||||
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
|
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
|
||||||
return super().server_bind()
|
return super().server_bind()
|
||||||
|
|
||||||
|
def finish_request(self, request, client_address):
|
||||||
|
self.RequestHandlerClass(request, client_address, self,
|
||||||
|
directory=args.directory)
|
||||||
|
|
||||||
test(
|
test(
|
||||||
HandlerClass=handler_class,
|
HandlerClass=handler_class,
|
||||||
ServerClass=DualStackServer,
|
ServerClass=DualStackServer,
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix command-line option ``-d``/``--directory`` in module :mod:`http.server`
|
||||||
|
which is ignored when combined with command-line option ``--cgi``. Patch by
|
||||||
|
Géry Ogam.
|
Loading…
Add table
Add a link
Reference in a new issue