Skip to content

Commit 433433f

Browse files
authored
Adds IPv6 support when invoking http.server directly. (GH-10595)
1 parent 75e4699 commit 433433f

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Doc/library/http.server.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,18 @@ the previous example, this serves files relative to the current directory::
410410
python -m http.server 8000
411411

412412
By default, server binds itself to all interfaces. The option ``-b/--bind``
413-
specifies a specific address to which it should bind. For example, the
414-
following command causes the server to bind to localhost only::
413+
specifies a specific address to which it should bind. Both IPv4 and IPv6
414+
addresses are supported. For example, the following command causes the server
415+
to bind to localhost only::
415416

416417
python -m http.server 8000 --bind 127.0.0.1
417418

418419
.. versionadded:: 3.4
419420
``--bind`` argument was introduced.
420421

422+
.. versionadded:: 3.8
423+
``--bind`` argument enhanced to support IPv6
424+
421425
By default, server uses the current directory. The option ``-d/--directory``
422426
specifies a directory to which it should serve the files. For example,
423427
the following command uses a specific directory::

Lib/http/server.py

+3
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,9 @@ def test(HandlerClass=BaseHTTPRequestHandler,
12261226
"""
12271227
server_address = (bind, port)
12281228

1229+
if ':' in bind:
1230+
ServerClass.address_family = socket.AF_INET6
1231+
12291232
HandlerClass.protocol_version = protocol
12301233
with ServerClass(server_address, HandlerClass) as httpd:
12311234
sa = httpd.socket.getsockname()

Lib/test/test_httpservers.py

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from http import server, HTTPStatus
1010

1111
import os
12+
import socket
1213
import sys
1314
import re
1415
import base64
@@ -1116,6 +1117,24 @@ def test_all(self):
11161117
self.assertCountEqual(server.__all__, expected)
11171118

11181119

1120+
class ScriptTestCase(unittest.TestCase):
1121+
@mock.patch('builtins.print')
1122+
def test_server_test_ipv6(self, _):
1123+
mock_server = mock.MagicMock()
1124+
server.test(ServerClass=mock_server, bind="::")
1125+
self.assertEqual(mock_server.address_family, socket.AF_INET6)
1126+
1127+
mock_server.reset_mock()
1128+
server.test(ServerClass=mock_server,
1129+
bind="2001:0db8:85a3:0000:0000:8a2e:0370:7334")
1130+
self.assertEqual(mock_server.address_family, socket.AF_INET6)
1131+
1132+
mock_server.reset_mock()
1133+
server.test(ServerClass=mock_server,
1134+
bind="::1")
1135+
self.assertEqual(mock_server.address_family, socket.AF_INET6)
1136+
1137+
11191138
def test_main(verbose=None):
11201139
cwd = os.getcwd()
11211140
try:
@@ -1127,6 +1146,7 @@ def test_main(verbose=None):
11271146
CGIHTTPServerTestCase,
11281147
SimpleHTTPRequestHandlerTestCase,
11291148
MiscTestCase,
1149+
ScriptTestCase
11301150
)
11311151
finally:
11321152
os.chdir(cwd)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adds IPv6 support when invoking http.server directly.

0 commit comments

Comments
 (0)