Python: Valid a IP address
Python Basic: Exercise-139 with Solution
Write a Python program to valid a IP address.
Sample Solution:-
import socket
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError: # no inet_pton here, sorry
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
address='127.0.0.1'
if is_valid_ipv4_address(address):
print("Valid IP")
addr = '127.0.0.2561'
try:
socket.inet_aton(addr)
except socket.error:
print("Invalid IP")
========== RESTART: F:/Python_APSC/py-ex-basic-139.py =========
Valid IP
Invalid IP
>>>
沒有留言:
張貼留言