Open
Description
Feature or enhancement
Proposal:
In stdlib http.cookiejar
we've got a IPV4_RE
constant:
IPV4_RE = re.compile(r"\.\d+$", re.ASCII)
it is widely used in deciding whether a string is a HDN or a IP address. such as:
def liberal_is_HDN(text):
"""Return True if text is a sort-of-like a host domain name.
For accepting/blocking domains.
"""
if IPV4_RE.search(text):
return False
return True
but the regex only checks if the string ends with a dot and some numbers. IMO
- Firstly, we could add number ranges in the regex as numbers in IPv4 couldn't be bigger than 255
- Secondly, it does not support IPv6, address like
2001:db8::1
would be detected as a HDN which is completely wrong.
The author also stated that in is_HDN()
:
# XXX
# This may well be wrong. Which RFC is HDN defined in, if any (for
# the purposes of RFC 2965)?
# For the current implementation, what about IPv6? Remember to look
# at other uses of IPV4_RE also, if change this.
Since we've got ipaddress
lib. I think we can use ipaddress to decide whether a string is a IP address or a HDN.
def is_ip(text):
"""Return True if text is a valid IP address."""
# This function is a replacement of regex `IPV4_RE` in previous versions.
try:
ip_address(text)
return True
except ValueError:
return False
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse