regex - python: check if IP or DNS -


How can one verify that the variable has the DNS name or IP address in the dragon?

You can use the module of Python to see if the content of the variable is the IP address .

For example IP address:

  import my_ip = "192.168.1.1" is_valid = re.match ("^ (([0-9] | [1- 9] [0-9] | 1 [0- 9] {2} | 2 [0-4] [0-9] | 25 [0-5]) \) {3} ([0-9] | [1-9] [0-9] | 1 [0-9] [2} | 2 [0-4] [0- 9] | 25 [0-5]) $ ", my_ip) if_valid: print" % S is a valid IP address "% my_ip   

Example for the host name:

  import my_hostname =" testhostname "is_valid = re.match (" ^ ([[A-zA-Z] | [a-zA-Z] [one-zA-Z0-9 \ -] * [one-zA-Z0-9])) * ([a-zA-Z ] [A-za-z] [A-Za-z0-9 \ -] * [A-Za-z0- 9]) $ ", my_hostname) if is_valid: print"% s is a valid hostname "% My_hostname    

Comments