thereal90
thereal90

Reputation: 105

Is there a way of comparing two dictionaries using the values of the keys?

I am working on two dictionaries. The first dictionary "Blacklist" contains addresses as key and dates as values. The second dictionary "transferred" also contains addresses as key and dates as values. I want to compare the two dictionaries to check if any of the "transferred" address is in the "blacklist" address. if there is a match, i want to check if the date on the blacklist was before or after the transferred date. I want it to create a new dictionary "update" with address as key and value to be "before" or "after" as the case may be.

Please take note "94.142.136.0/21" matches "94.142.136.190"

blacklist = {'93.118.36.235': '25/02/2016', '62.149.128.160': '05/06/2017', '62.149.128.163': '05/06/2017', '62.149.128.166': '05/06/2017', '62.149.128.72': '05/06/2017', '62.149.128.74': '05/06/2017', '69.163.171.33': '10/03/2014', '69.163.200.61': '22/12/2014', '94.142.136.190': '19/02/2016'}

transferred ={'94.142.136.0/21': '28/06/2019', '185.2.4.0/22': '01/07/2019', '213.158.64.0/19': '01/07/2019', '5.154.240.0/24': '01/07/2019', '78.159.140.0/22': '01/07/2019', '81.88.48.0/20': '01/07/2019'}

found = {}

for k,v in blacklist.items():
     for k1,v1 in transferred.items():
         if k1 = k:
            if v1 > v:
               found.append(k1, 'before')
            else:
               found.append(k1, 'after')

I expect this as the result

found = {'94.142.136.0/21': 'before'}

Upvotes: 0

Views: 121

Answers (3)

prithajnath
prithajnath

Reputation: 2115

First, you need to check if the IP address in the blacklist dictionary is in one of the CIDR blocks in the transferred dictionary. Then you need to convert those date strings to datetime objects so you can compare them

from ipaddress import ip_address, ip_network
from datetime import date

blacklist = {'93.118.36.235': '25/02/2016', '62.149.128.160': '05/06/2017', '62.149.128.163': '05/06/2017', '62.149.128.166': '05/06/2017', '62.149.128.72': '05/06/2017', '62.149.128.74': '05/06/2017', '69.163.171.33': '10/03/2014', '69.163.200.61': '22/12/2014', '94.142.136.190': '19/02/2016'}

transferred ={'94.142.136.0/21': '28/06/2019', '185.2.4.0/22': '01/07/2019', '213.158.64.0/19': '01/07/2019', '5.154.240.0/24': '01/07/2019', '78.159.140.0/22': '01/07/2019', '81.88.48.0/20': '01/07/2019'}

found = {}

for ip_addr in blacklist:
    for cidr_block in transferred:
        if ip_address(ip_addr) in ip_network(cidr_block):
            blacklist_date = date(*map(int,blacklist[ip_addr].split("/")[::-1]))
            transferred_date = date(*map(int,transferred[cidr_block].split("/")[::-1]))

            if transferred_date > blacklist_date:
                found[cidr_block] =  'before'
            else:
                found[cidr_block] =  'after'

print(found)

Upvotes: 1

hbwales
hbwales

Reputation: 193

the ipaddress module seems to do what you want: []https://docs.python.org/3/library/ipaddress.html

from ipaddress import ip_network, ip_address

found = {}
for k,v in blacklist.items():
    for k1,v1 in transferred.items():
        if ip_address(k) in ip_network(k1):
            found[k1] = 'before' if v1 > v else 'after'

Upvotes: 0

LazyCoder
LazyCoder

Reputation: 1265

Use these methods to convert the formats, check if they are equal.

import socket,struct

def makeMask(n):
    "return a mask of n bits as a long integer"
    return (2L<<n-1) - 1

def dottedQuadToNum(ip):#Pass just 94.142.136.190
    "convert decimal dotted quad string to long integer"
    return struct.unpack('L',socket.inet_aton(ip))[0]

def networkMask(ip,bits):#Pass 94.142.136.0 and 21
    "Convert a network address to a long integer" 
    return dottedQuadToNum(ip) & makeMask(bits)

Upvotes: 0

Related Questions