Commit 433433fa authored by Lisa Roach's avatar Lisa Roach Committed by GitHub

Adds IPv6 support when invoking http.server directly. (GH-10595)

parent 75e4699b
...@@ -410,14 +410,18 @@ the previous example, this serves files relative to the current directory:: ...@@ -410,14 +410,18 @@ the previous example, this serves files relative to the current directory::
python -m http.server 8000 python -m http.server 8000
By default, server binds itself to all interfaces. The option ``-b/--bind`` By default, server binds itself to all interfaces. The option ``-b/--bind``
specifies a specific address to which it should bind. For example, the specifies a specific address to which it should bind. Both IPv4 and IPv6
following command causes the server to bind to localhost only:: 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 python -m http.server 8000 --bind 127.0.0.1
.. versionadded:: 3.4 .. versionadded:: 3.4
``--bind`` argument was introduced. ``--bind`` argument was introduced.
.. versionadded:: 3.8
``--bind`` argument enhanced to support IPv6
By default, server uses the current directory. The option ``-d/--directory`` By default, server uses the current directory. The option ``-d/--directory``
specifies a directory to which it should serve the files. For example, specifies a directory to which it should serve the files. For example,
the following command uses a specific directory:: the following command uses a specific directory::
......
...@@ -1226,6 +1226,9 @@ def test(HandlerClass=BaseHTTPRequestHandler, ...@@ -1226,6 +1226,9 @@ def test(HandlerClass=BaseHTTPRequestHandler,
""" """
server_address = (bind, port) server_address = (bind, port)
if ':' in bind:
ServerClass.address_family = socket.AF_INET6
HandlerClass.protocol_version = protocol HandlerClass.protocol_version = protocol
with ServerClass(server_address, HandlerClass) as httpd: with ServerClass(server_address, HandlerClass) as httpd:
sa = httpd.socket.getsockname() sa = httpd.socket.getsockname()
......
...@@ -9,6 +9,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer, \ ...@@ -9,6 +9,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer, \
from http import server, HTTPStatus from http import server, HTTPStatus
import os import os
import socket
import sys import sys
import re import re
import base64 import base64
...@@ -1116,6 +1117,24 @@ class MiscTestCase(unittest.TestCase): ...@@ -1116,6 +1117,24 @@ class MiscTestCase(unittest.TestCase):
self.assertCountEqual(server.__all__, expected) 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): def test_main(verbose=None):
cwd = os.getcwd() cwd = os.getcwd()
try: try:
...@@ -1127,6 +1146,7 @@ def test_main(verbose=None): ...@@ -1127,6 +1146,7 @@ def test_main(verbose=None):
CGIHTTPServerTestCase, CGIHTTPServerTestCase,
SimpleHTTPRequestHandlerTestCase, SimpleHTTPRequestHandlerTestCase,
MiscTestCase, MiscTestCase,
ScriptTestCase
) )
finally: finally:
os.chdir(cwd) os.chdir(cwd)
......
Adds IPv6 support when invoking http.server directly.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment