1. 判断IP 格式是否正确
    def check_value(self, ipaddr):
        '''检查IP是否合法
        :param ipaddr:  string 
        :return True
        '''
        addr=ipaddr.strip().split('.')
        if len(addr) != 4:
            return False
        for i in range(4):
            try:
                addr[i]=int(addr[i])
            except:
                return False
            if addr[i]<=255 and addr[i]>=0:
                pass
            else:
                return False
            i+=1
        else:
            return True
    
     
    
  2. 判断是否为公网IP
    from IPy import IP
    if self.check_value(value):
        if IP(value).iptype() is 'PUBLIC':
            return True
    

     


    或则直接用netaddr

    from netaddr import *
    ip = IPAddress(value)
    if ip.is_unicast() and not ip.is_private():
    	return True