diff -r 9e5bc3d38de8 -r 2c8b64e00097 Lib/http/server.py --- a/Lib/http/server.py Mon Dec 05 16:59:22 2016 +1000 +++ b/Lib/http/server.py Mon Dec 05 12:05:14 2016 +0100 @@ -103,6 +103,7 @@ import urllib.parse import copy import argparse +from functools import partial from http import HTTPStatus @@ -634,6 +635,12 @@ server_version = "SimpleHTTP/" + __version__ + def __init__(self, *args, directory=None, **kwargs): + if directory is None: + directory = os.getcwd() + self.directory = directory + super().__init__(*args, **kwargs) + def do_GET(self): """Serve a GET request.""" f = self.send_head() @@ -776,7 +783,7 @@ path = posixpath.normpath(path) words = path.split('/') words = filter(None, words) - path = os.getcwd() + path = self.directory for word in words: if os.path.dirname(word) or word in (os.curdir, os.pardir): # Ignore components that are not a simple file/directory name @@ -1206,9 +1213,13 @@ default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') + parser.add_argument('--directory', '-d', default=os.getcwd(), + help='Specify alternate directory ' + '[default: current directory]') args = parser.parse_args() if args.cgi: handler_class = CGIHTTPRequestHandler else: - handler_class = SimpleHTTPRequestHandler + handler_class = partial(SimpleHTTPRequestHandler, + directory=args.directory) test(HandlerClass=handler_class, port=args.port, bind=args.bind)