From: Bob Halley Date: Mon, 18 May 2020 15:07:47 +0000 (-0700) Subject: add is_address() X-Git-Tag: v2.0.0rc1~196^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=037dfe7f71f21e37fade0e3a203e38b675a1aa43;p=thirdparty%2Fdnspython.git add is_address() --- diff --git a/dns/inet.py b/dns/inet.py index 62719dc9..254b5d07 100644 --- a/dns/inet.py +++ b/dns/inet.py @@ -120,3 +120,22 @@ def is_multicast(text): return first == 255 except Exception: raise ValueError + + +def is_address(text): + """Is the specified string an IPv4 or IPv6 address? + + *text*, a ``str``, the textual address. + + Returns a ``bool``. + """ + + try: + dns.ipv4.inet_aton(text) + return True + except Exception: + try: + dns.ipv6.inet_aton(text, True) + return True + except Exception: + raise False