Skip to content

Commit 0879750

Browse files
authored
update is_ip
1 parent 3d1cc91 commit 0879750

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Lib/http/cookiejar.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,19 @@ def parse_ns_headers(ns_headers):
537537

538538
def is_ip(text: str):
539539
"""Return True if text is a valid IP address."""
540-
from ipaddress import ip_address
541-
text = text.removeprefix('[').removesuffix(']')
540+
from ipaddress import IPv4Address, IPv6Address
541+
# check for IPv4 address
542542
try:
543-
ip_address(text)
543+
IPv4Address(text)
544544
except ValueError:
545-
return False
545+
# check for IPv6 address in []
546+
if text.startswith('[') and text.endswith(']'):
547+
try:
548+
IPv6Address(text.removeprefix('[').removesuffix(']'))
549+
except ValueError:
550+
return False
551+
else:
552+
return False # not a IPv6 address in []
546553
return True
547554
def is_HDN(text):
548555
"""Return True if text is a host domain name."""

0 commit comments

Comments
 (0)