Example of how to use Python to determine the legitimacy of IP addresses

Source: Internet
Author: User
First, the use of methods and performance, see the Picture:

Second, Python implementation code:
Copy the Code code as follows:

[Root@yang python]# VI check_ip.py
#!/usr/bin/python
Import Os,sys
def check_ip (ipaddr):
Import Sys
Addr=ipaddr.strip (). Split ('. ') #切割IP地址为一个列表
#print Addr
If Len (addr)! = 4: #切割后列表必须有4个参数
print "Check IP address failed!"
Sys.exit ()
For I in range (4):
Try
Addr[i]=int (Addr[i]) #每个参数必须为数字, otherwise the checksum fails
Except
print "Check IP address failed!"
Sys.exit ()
If addr[i]<=255 and addr[i]>=0: between #每个参数值必须在0-255
Pass
Else
print "Check IP address failed!"
Sys.exit ()
I+=1
Else
print "Check IP address success!"
If Len (sys.argv)!=2: #传参加本身长度必须为2
Print "Example:%s 10.0.0.1"%sys.argv[0]
Sys.exit ()
Else
CHECK_IP (sys.argv[1]) #满足条件调用校验IP函数

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.