w3resource
w3resource logo
Python Exercises

Python: Valid an IP address

Python Basic: Exercise-139 with Solution

Write a Python program to valid an IP address.

Sample Solution:-

Python Code:

import socket
addr = '127.0.0.2561'
try:
    socket.inet_aton(addr)
    print("Valid IP")
except socket.error:
    print("Invalid IP")
	

Sample Output:

Invalid IP  

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to convert true to 1 and false to 0.
Next: Write a Python program to convert an integer to binary keep leading zeros.