Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.9] bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701) #31103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@
import sys
import time
import urllib.parse
import contextlib
from functools import partial

from http import HTTPStatus

Expand Down Expand Up @@ -1240,7 +1238,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]
Expand All @@ -1257,36 +1254,40 @@ 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):
self.socket.setsockopt(
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.