Determine if Host is a domain name or IP address in Python

Is there a function in Python that determines if a hostname is a domain name or an IP (v4) address?

Note. The domain name may look like alex.foo.bar.com

or even (I think it is really) 1.2.3.com

.

+3


source to share


1 answer


I would use IPy to check if a string is an IP address, and if not, let's say it's a domain name. For example:.



from IPy import IP
def isIP(str):
    try:
        IP(str)
    except ValueError:
        return False
    return True

      

0


source







All Articles