diff --git a/Lib/http/server.py b/Lib/http/server.py index e12e45b..7e2a01c 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -631,6 +631,20 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): server_version = "SimpleHTTP/" + __version__ + def __init__(self, *args, **kwargs): + if 'directory' in kwargs: + self.directory = kwargs.pop('directory') + else: + self.directory = os.getcwd() + super().__init__(*args, **kwargs) + + @classmethod + def from_directory(cls, directory): + def ctor(*args, **kwargs): + kwargs['directory'] = directory + return cls(*args, **kwargs) + return ctor + def do_GET(self): """Serve a GET request.""" f = self.send_head() @@ -773,7 +787,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): 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 @@ -1203,9 +1217,12 @@ if __name__ == '__main__': 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 = SimpleHTTPRequestHandler.from_directory(args.directory) test(HandlerClass=handler_class, port=args.port, bind=args.bind)