Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
True
>>> parse_host('192.168.1.1') == (socket.AF_INET, '192.168.1.1')
True
"""
if not host:
return None, u''
if u':' in host:
ipv6_match = _IP_LITERAL_RE.match(host)
if ipv6_match is None:
raise URLParseError(u'invalid IPv6 host: %r' % host)
ipv4_match = IPv4_PART_RE.search(host)
if ipv4_match:
try:
socket.inet_aton(ipv4_match.group(0))
except socket.error: # NB: socket.error _is_ OSError on Py3
raise URLParseError(u'invalid IPv6 host with IPv4: %r' % host)
return socket.AF_INET6, host
# This is necessary because inet_aton takes non-quad inputs see
# the man page for inet
family = None
ipv4_match = IPv4_RE.search(host)
if ipv4_match:
try:
socket.inet_aton(host)
except (socket.error, UnicodeEncodeError):
# inet_aton raises socket.error on py2, OSError on py3
# UnicodeEncodeError is only reached on non-ASCII unicode hosts
pass # regular domain/host name, needs resolution
else:
family = socket.AF_INET
return family, host