]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Adds IPv6 support when invoking http.server directly. (GH-10595)
authorLisa Roach <lisaroach14@gmail.com>
Mon, 26 Nov 2018 18:43:38 +0000 (10:43 -0800)
committerGitHub <noreply@github.com>
Mon, 26 Nov 2018 18:43:38 +0000 (10:43 -0800)
Doc/library/http.server.rst
Lib/http/server.py
Lib/test/test_httpservers.py
Misc/NEWS.d/next/Library/2018-11-18-18-44-40.bpo-24209.p3YWOf.rst [new file with mode: 0644]

index 29f6e7da3bde537bc3ca5c3829bf2d04f06162d5..a367e373dc3c03b0b4223ab0782355dc348caad9 100644 (file)
@@ -410,14 +410,18 @@ the previous example, this serves files relative to the current directory::
         python -m http.server 8000
 
 By default, server binds itself to all interfaces.  The option ``-b/--bind``
-specifies a specific address to which it should bind.  For example, the
-following command causes the server to bind to localhost only::
+specifies a specific address to which it should bind. Both IPv4 and IPv6
+addresses are supported. For example, the following command causes the server
+to bind to localhost only::
 
         python -m http.server 8000 --bind 127.0.0.1
 
 .. versionadded:: 3.4
     ``--bind`` argument was introduced.
 
+.. versionadded:: 3.8
+    ``--bind`` argument enhanced to support IPv6
+
 By default, server uses the current directory. The option ``-d/--directory``
 specifies a directory to which it should serve the files. For example,
 the following command uses a specific directory::
index ca2dd507392c8ec8de7eb94ede1efbe7f20cb02b..22d865f2fdfa9261aaf9173b21ad66f8059c26c1 100644 (file)
@@ -1226,6 +1226,9 @@ def test(HandlerClass=BaseHTTPRequestHandler,
     """
     server_address = (bind, port)
 
+    if ':' in bind:
+        ServerClass.address_family = socket.AF_INET6
+
     HandlerClass.protocol_version = protocol
     with ServerClass(server_address, HandlerClass) as httpd:
         sa = httpd.socket.getsockname()
index cc829a522b88bc3d29c9a6b720c28648dd465bc0..3d8e0af8b45c274f3d5a8f802fb87a47761db3e9 100644 (file)
@@ -9,6 +9,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer, \
 from http import server, HTTPStatus
 
 import os
+import socket
 import sys
 import re
 import base64
@@ -1116,6 +1117,24 @@ class MiscTestCase(unittest.TestCase):
         self.assertCountEqual(server.__all__, expected)
 
 
+class ScriptTestCase(unittest.TestCase):
+    @mock.patch('builtins.print')
+    def test_server_test_ipv6(self, _):
+        mock_server = mock.MagicMock()
+        server.test(ServerClass=mock_server, bind="::")
+        self.assertEqual(mock_server.address_family, socket.AF_INET6)
+
+        mock_server.reset_mock()
+        server.test(ServerClass=mock_server,
+                    bind="2001:0db8:85a3:0000:0000:8a2e:0370:7334")
+        self.assertEqual(mock_server.address_family, socket.AF_INET6)
+
+        mock_server.reset_mock()
+        server.test(ServerClass=mock_server,
+                    bind="::1")
+        self.assertEqual(mock_server.address_family, socket.AF_INET6)
+
+
 def test_main(verbose=None):
     cwd = os.getcwd()
     try:
@@ -1127,6 +1146,7 @@ def test_main(verbose=None):
             CGIHTTPServerTestCase,
             SimpleHTTPRequestHandlerTestCase,
             MiscTestCase,
+            ScriptTestCase
         )
     finally:
         os.chdir(cwd)
diff --git a/Misc/NEWS.d/next/Library/2018-11-18-18-44-40.bpo-24209.p3YWOf.rst b/Misc/NEWS.d/next/Library/2018-11-18-18-44-40.bpo-24209.p3YWOf.rst
new file mode 100644 (file)
index 0000000..88749e9
--- /dev/null
@@ -0,0 +1 @@
+Adds IPv6 support when invoking http.server directly.