Move None check outside is_ipadress()
MODIFY
test/test_ssl.py
test/test_ssl.py
@@ -53,8 +53,8 @@ def test_context_sni_with_ip_address(monkeypatch, has_sni, server_hostname, uses
(True, '127.0.0.1', False),
(False, '127.0.0.1', False),
(False, 'www.google.com', True),
- (True, None, True),
- (False, None, True)]
+ (True, None, False),
+ (False, None, False)]
)
def test_sni_missing_warning_with_ip_addresses(monkeypatch, has_sni, server_hostname, should_warn):
monkeypatch.setattr(ssl_, 'HAS_SNI', has_sni)

MODIFY
urllib3/util/ssl_.py
urllib3/util/ssl_.py
@@ -62,7 +62,6 @@ except ImportError:
if hasattr(socket, 'inet_pton'):
inet_pton = socket.inet_pton
else:
-
# Maybe we can use ipaddress if the user has urllib3[secure]?
try:
import ipaddress
@@ -351,7 +350,7 @@ def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,
# extension should not be used according to RFC3546 Section 3.1
# We shouldn't warn the user if SNI isn't available but we would
# not be using SNI anyways due to IP address for server_hostname.
- if not is_ipaddress(server_hostname):
+ if server_hostname is not None and not is_ipaddress(server_hostname):
if HAS_SNI and server_hostname is not None:
return context.wrap_socket(sock, server_hostname=server_hostname)
@@ -375,8 +374,6 @@ def is_ipaddress(hostname):
:param str hostname: Hostname to examine.
:return: True if the hostname is an IP address, False otherwise.
"""
- if hostname is None:
- return False
if six.PY3 and isinstance(hostname, six.binary_type):
# IDN A-label bytes are ASCII compatible.
hostname = hostname.decode('ascii')
@@ -388,7 +385,7 @@ def is_ipaddress(hostname):
for af in families:
try:
inet_pton(af, hostname)
- except (socket.error, ValueError):
+ except (socket.error, ValueError, OSError):
pass
else:
return True

Move None check outside is_ipadress()