Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if Host is Domain Name or IP 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 this is valid): 1.2.3.com.

like image 235
Alex Rothberg Avatar asked Oct 21 '25 21:10

Alex Rothberg


1 Answers

I'd use IPy to test if the string is an IP address, and if it isn't - assume it's a domain name. E.g.:

from IPy import IP
def isIP(str):
    try:
        IP(str)
    except ValueError:
        return False
    return True
like image 146
Mureinik Avatar answered Oct 24 '25 09:10

Mureinik