Example of a method that USES Python to determine the validity of an IP address
- 2020-04-02 13:32:26
- OfStack
I. use method and implementation effect see the figure:
< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201403/201431392344653.jpg? 20142139244 ">
Ii. Python implementation code
[root@yang python]# vi check_ip.py
#!/usr/bin/python
import os,sys
def check_ip(ipaddr):
import sys
addr=ipaddr.strip().split('.') # cutting IP The address is a list
#print addr
if len(addr) != 4: # After cutting the list must have 4 A parameter
print "check ip address failed!"
sys.exit()
for i in range(4):
try:
addr[i]=int(addr[i]) # Each parameter must be a number, otherwise the check fails
except:
print "check ip address failed!"
sys.exit()
if addr[i]<=255 and addr[i]>=0: # Each parameter value must be in 0-255 between
pass
else:
print "check ip address failed!"
sys.exit()
i+=1
else:
print "check ip address success!"
if len(sys.argv)!=2: # The length of the pass itself must be 2
print "Example: %s 10.0.0.1 "%sys.argv[0]
sys.exit()
else:
check_ip(sys.argv[1]) # It satisfies the condition to call the checksum IP function