Skip to content

bpo-38907: Add IPv6 Dual-Stack control for http.server #17378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@
class HTTPServer(socketserver.TCPServer):

allow_reuse_address = 1 # Seems to make sense in testing environment
no_dual_stack = 0

def server_bind(self):
"""Override server_bind to store the server name."""
"""Override server_bind to store the server name and
deal with Dual Stack."""
if self.address_family == socket.AF_INET6 and socket.has_dualstack_ipv6():
self.socket.setsockopt(socket.IPPROTO_IPV6,
socket.IPV6_V6ONLY,
self.no_dual_stack)
socketserver.TCPServer.server_bind(self)
host, port = self.server_address[:2]
self.server_name = socket.getfqdn(host)
Expand Down Expand Up @@ -1238,13 +1244,14 @@ def _get_best_family(*address):

def test(HandlerClass=BaseHTTPRequestHandler,
ServerClass=ThreadingHTTPServer,
protocol="HTTP/1.0", port=8000, bind=None):
protocol="HTTP/1.0", port=8000, bind=None, no_dual_stack=False):
"""Test the HTTP request handler class.

This runs an HTTP server on port 8000 (or the port argument).

"""
ServerClass.address_family, addr = _get_best_family(bind, port)
ServerClass.no_dual_stack = int(no_dual_stack)

HandlerClass.protocol_version = protocol
with ServerClass(addr, HandlerClass) as httpd:
Expand All @@ -1269,6 +1276,8 @@ def test(HandlerClass=BaseHTTPRequestHandler,
parser.add_argument('--bind', '-b', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
parser.add_argument('--no-dual-stack', action='store_true',
help='Do not use IPv4/IPv6 Dual Stack')
parser.add_argument('--directory', '-d', default=os.getcwd(),
help='Specify alternative directory '
'[default:current directory]')
Expand All @@ -1282,4 +1291,5 @@ def test(HandlerClass=BaseHTTPRequestHandler,
else:
handler_class = partial(SimpleHTTPRequestHandler,
directory=args.directory)
test(HandlerClass=handler_class, port=args.port, bind=args.bind)
test(HandlerClass=handler_class, port=args.port, bind=args.bind,
no_dual_stack=args.no_dual_stack)