]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
add is_address()
authorBob Halley <halley@dnspython.org>
Mon, 18 May 2020 15:07:47 +0000 (08:07 -0700)
committerBob Halley <halley@dnspython.org>
Mon, 18 May 2020 15:07:47 +0000 (08:07 -0700)
dns/inet.py

index 62719dc9931f681787becc7291cfebfa0dd1de55..254b5d079f662d824a198ae29faff146967dca8d 100644 (file)
@@ -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