两个ip数组作比较

#!/usr/bin/python

def msg_ip_node_set(node, tmp_ip_node):
    if not node:
        return
    action = node.get("action", "normal")

    if "del" == action:
        tmp_ip_node['action'] = 0
    elif "add" == action:
        tmp_ip_node['action'] = 1
    else:#normal
        return False

    #tmp_ip_node['type'] = node['type']
    tmp_ip_node['ip'] = node['ip']

    #if node["ip_type"]:
    #    tmp_ip_node['mask'] = node['mask']##prefixlen()
    #else:
    #    tmp_ip_node['mask'] = IP(node["ip"]).make_net(node["mask"]).strNetmask()
    #    tmp_ip_node["ip_type"] = 0 #only support ipv4


    print "tmp_ip_node=======", tmp_ip_node

    return True

def inf_ip_set_node(ips_new, ips_old, tmp_ip_list, flag=False):
    ''' flag==True add ip ,flag==false False del ip'''
    tmp_i=0
    
    equal_flag = 0
    for ip in ips_new:    
        
        tmp_ip_node = {}
        if flag == True:
            for ip_old in ips_old:
                if ip['ip'] == ip_old['ip']:
                  
                    print "new equal 1 2 ", ip['ip'], ip_old['ip']
                    equal_flag = 1
                    break;
                else:
                    equal_flag = 0
                    continue

            
        if equal_flag == 0:            
            ip['action'] = 'add'
            if not msg_ip_node_set(ip, tmp_ip_node):
                continue

            tmp_ip_list.append(tmp_ip_node)
            tmp_action = tmp_ip_node.get('action')

            if tmp_action:
                if tmp_i == 0:
                    print('inf_ip_action')
                #modify_log('inf_ip_addr', tmp_ip_node['ip'], tmp_ip_node['mask'])
                #modify_log('add' if tmp_action == 1 else 'del')
                tmp_i = 1
            if tmp_i == 1:
                print('inf_ip_addr_end')
                
        else:
            pass
            
    equal_flag = 0
    for ip in ips_old:
    
        tmp_ip_node = {}
        if flag == True:
            for ip_new in ips_new:
                if ip['ip'] == ip_new['ip']:
                    equal_flag = 1
                    break;
                else:
                    equal_flag = 0
                    continue

            
        if equal_flag == 0:            
            ip['action'] = 'del'
            if not msg_ip_node_set(ip, tmp_ip_node):
                continue

            tmp_ip_list.append(tmp_ip_node)
            tmp_action = tmp_ip_node.get('action')

            if tmp_action:
                if tmp_i == 0:
                    print('inf_ip_action')
                #modify_log('inf_ip_addr', tmp_ip_node['ip'], tmp_ip_node['mask'])
                #modify_log('add' if tmp_action == 1 else 'del')
                tmp_i = 1
            if tmp_i == 1:
                print('inf_ip_addr_end')
                
        else:
            pass
                        

    return tmp_ip_list



if __name__ == "__main__":
    print "====================test================"

    ips_new = [{'ip': '1.1.1.1'},{'ip': '2.2.2.2'},{'ip': '5.5.5.5'},{'ip': '6.6.6.6'}]
    ips_old = [{'ip': '3.3.3.3'},{'ip': '2.2.2.2'},{'ip': '5.5.5.9'},{'ip': '5.5.5.5'}]
    tmp_ip_list = []

    result = inf_ip_set_node(ips_new, ips_old, tmp_ip_list, True)

    print "##############################", result

posted on 2017-04-13 15:52  与非朋仔  阅读(164)  评论(0)    收藏  举报

导航