二、python实现代码:
复制代码 代码如下:

[ro">

使用Python判断IP地址合法性的方法实例

WBOY
Release: 2016-06-16 08:44:54
Original
2120 people have browsed it

一、使用方法和执行效果请看图:
使用Python判断IP地址合法性的方法实例
二、python实现代码:

复制代码 代码如下:
[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])  #每个参数必须为数字,否则校验失败
                except:
                        print "check ip address failed!"
                        sys.exit()
                if addr[i]=0:    #每个参数值必须在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函数
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!