Skip to content

Commit

Permalink
Use ipaddress/inet_aton in place of inet_pton
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Apr 25, 2018
1 parent 4d5dbee commit 4c0fc3c
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions urllib3/util/ssl_.py
Expand Up @@ -55,6 +55,25 @@ def _const_compare_digest_backport(a, b):
OP_NO_SSLv2, OP_NO_SSLv3 = 0x1000000, 0x2000000
OP_NO_COMPRESSION = 0x20000


# Python 2.7 and earlier didn't have inet_pton on non-Linux
# so we fallback on inet_aton in those cases. This means that
# we can only detect IPv4 addresses in this case.
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

def inet_pton(_, host):
return ipaddress.ip_address(host)
except ImportError: # Platform-specific: Non-Linux
def inet_pton(_, host):
return socket.inet_aton(host)


# A secure default.
# Sources for more information on TLS ciphers:
#
Expand Down Expand Up @@ -361,13 +380,15 @@ def is_ipaddress(hostname):
if six.PY3 and isinstance(hostname, six.binary_type):
# IDN A-label bytes are ASCII compatible.
hostname = hostname.decode('ascii')

families = [socket.AF_INET]
if hasattr(socket, 'AF_INET6'):
families.append(socket.AF_INET6)
families.append(socket.AF_INET6)

for af in families:
try:
socket.inet_pton(af, hostname)
except socket.error:
inet_pton(af, hostname)
except (socket.error, ValueError):
pass
else:
return True
Expand Down

0 comments on commit 4c0fc3c

Please sign in to comment.