-
Notifications
You must be signed in to change notification settings - Fork 33
Fix: Support IPv4/IPv6 dual-stack listening on Windows (Backport from CPython) #43
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,18 @@ | |
| from . import RangeRequestHandler | ||
|
|
||
| import argparse | ||
| import contextlib | ||
| import socket | ||
|
|
||
|
|
||
| class DualStackServer(SimpleHTTPServer.ThreadingHTTPServer): | ||
| def server_bind(self): | ||
| # suppress exception when protocol is IPv4 | ||
| with contextlib.suppress(Exception): | ||
| self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) | ||
| return super().server_bind() | ||
|
Comment on lines
+27
to
+32
|
||
|
|
||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('port', action='store', | ||
| default=8000, type=int, | ||
|
|
@@ -28,4 +40,4 @@ | |
| help='bind to this address (default: all interfaces)') | ||
|
|
||
| args = parser.parse_args() | ||
| SimpleHTTPServer.test(HandlerClass=RangeRequestHandler, port=args.port, bind=args.bind) | ||
| SimpleHTTPServer.test(HandlerClass=RangeRequestHandler, ServerClass=DualStackServer, port=args.port, bind=args.bind) | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contextlib.suppress(Exception)is overly broad here and can hide real bugs (e.g., programming errors or unexpected socket state). Narrow the suppression to the specific exception types expected from thissetsockoptcall (typicallyOSError/AttributeError, depending on platform) so genuine failures still surface.