diff -r cc0e72082e52 Doc/library/http.server.rst --- a/Doc/library/http.server.rst Tue Apr 16 11:19:11 2013 -0400 +++ b/Doc/library/http.server.rst Sun Apr 21 19:41:15 2013 +0200 @@ -368,6 +368,15 @@ python -m http.server 8000 +Per default the server is accessable from overall the network. You may want to +specify a bind address with a ``--bind ADDRESS`` argument to limit the server +to a interface. In the following example, the server and its served files are +only accessable from the local maschine. :: + + python -m http.server --bind 127.0.0.1 8000 + +.. versionadded:: 3.4 + .. class:: CGIHTTPRequestHandler(request, client_address, server) diff -r cc0e72082e52 Lib/http/server.py --- a/Lib/http/server.py Tue Apr 16 11:19:11 2013 -0400 +++ b/Lib/http/server.py Sun Apr 21 19:41:15 2013 +0200 @@ -1182,15 +1182,15 @@ self.log_message("CGI script exited OK") -def test(HandlerClass = BaseHTTPRequestHandler, - ServerClass = HTTPServer, protocol="HTTP/1.0", port=8000): +def test(HandlerClass=BaseHTTPRequestHandler, + ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""): """Test the HTTP request handler class. This runs an HTTP server on port 8000 (or the first command line argument). """ - server_address = ('', port) + server_address = (bind, port) HandlerClass.protocol_version = protocol httpd = ServerClass(server_address, HandlerClass) @@ -1208,12 +1208,16 @@ parser = argparse.ArgumentParser() parser.add_argument('--cgi', action='store_true', help='Run as CGI Server') + parser.add_argument('--bind', '-b', default='', metavar='ADDRESS', + help='Specify alternate bind address ' + '[default: all interfaces]') parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() if args.cgi: - test(HandlerClass=CGIHTTPRequestHandler, port=args.port) + handler_class = CGIHTTPRequestHandler else: - test(HandlerClass=SimpleHTTPRequestHandler, port=args.port) + handler_class = SimpleHTTPRequestHandler + test(HandlerClass=handler_class, port=args.port, bind=args.bind)