[Python-checkins] cpython: Expose --bind argument for http.server, enable http.server to bind to a user

senthil.kumaran python-checkins at python.org
Sun Sep 15 18:37:33 CEST 2013


http://hg.python.org/cpython/rev/a248655c8261
changeset:   85713:a248655c8261
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Sun Sep 15 09:37:27 2013 -0700
summary:
  Expose --bind argument for http.server, enable http.server to bind to a user
specified network interface.

Patch contributed by Malte Swart. Addresses issue #17764.

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

files:
  Doc/library/http.server.rst |   9 +++++++++
  Lib/http/server.py          |  14 +++++++++-----
  Misc/NEWS                   |   3 +++
  3 files changed, 21 insertions(+), 5 deletions(-)


diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -368,6 +368,15 @@
 
         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)
 
diff --git a/Lib/http/server.py b/Lib/http/server.py
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -1186,15 +1186,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)
@@ -1212,12 +1212,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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list