]> granicus.if.org Git - python/commitdiff
Expose --bind argument for http.server, enable http.server to bind to a user
authorSenthil Kumaran <senthil@uthcode.com>
Sun, 15 Sep 2013 16:37:27 +0000 (09:37 -0700)
committerSenthil Kumaran <senthil@uthcode.com>
Sun, 15 Sep 2013 16:37:27 +0000 (09:37 -0700)
specified network interface.

Patch contributed by Malte Swart. Addresses issue #17764.

HG :Enter commit message.  Lines beginning with 'HG:' are removed.

Doc/library/http.server.rst
Lib/http/server.py
Misc/NEWS

index 53139a223981a39ce737dc3490f04334445b7425..ca66c40716b0381e3f2fd8da9a3758125f2fa577 100644 (file)
@@ -368,6 +368,15 @@ the previous example, this serves files relative to the current directory. ::
 
         python -m http.server 8000
 
+By default, server binds itself to all interfaces. To restrict it to bind to a
+particular interface only, ``--bind ADDRESS`` argument can be used. For e.g, to
+restrict the server to bind only to localhost. ::
+
+        python -m http.server 8000 --bind 127.0.0.1
+
+.. versionadded:: 3.4
+    ``--bind`` argument was introduced.
+
 
 .. class:: CGIHTTPRequestHandler(request, client_address, server)
 
index 87d83784edf8280553fa572743e2869d472ce51a..2ddef52beb07b0e624ff5c537b5e379e839110ab 100644 (file)
@@ -1186,15 +1186,15 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
                 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)
@@ -1212,12 +1212,16 @@ if __name__ == '__main__':
     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)
index 8845e3df5fcaa1c3c77aa386a11ea0a032f58030..e4325674e2ac59cdb724c2b2dca434ec55466a0f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17764: Enable http.server to bind to a user specified network
+  interface.  Patch contributed by Malte Swart.
+
 - Issue #18937: Add an assertLogs() context manager to unittest.TestCase
   to ensure that a block of code emits a message using the logging module.