I have created below Script processing two files based on user input and generating third result file.
Scripts executes properly without any issue but when both file having high count then it is taking time. During my testing i have tested with InputFile-1 having 500000 records and InputFile-2 having 100 records.
So wanted to check if there is any possibility of optimization reducing overall execution time. Kindly share your thoughts.!
Thanks in advance.
import ipaddress
filePathName1 = raw_input('InputFile-1 : ')
filePathName2 = raw_input('InputFile-2: ')
ipLookupResultFileName = filePathName1 + ' - ResultFile.txt'
ipLookupResultFile = open(ipLookupResultFileName,'w+')
with open(filePathName1,'r') as ipFile:
with open(filePathName2,'r') as ipCidrRangeFile:
for everyIP in ipFile:
ipLookupFlag = 'NONE'
ipCidrRangeFile.seek(0)
for everyIpCidrRange in ipCidrRangeFile:
if (ipaddress.IPv4Address(unicode(everyIP.rstrip('\n'))) in ipaddress.ip_network(unicode(everyIpCidrRange.rstrip('\n')))) == True:
ipLookupFlag = 'True'
break
if ipLookupFlag == 'True':
ipLookupResultFile.write(everyIP.rstrip('\n') + ' - Valid_Operator_IP' + '\n')
else:
ipLookupResultFile.write(everyIP.rstrip('\n') + ' - Not_Valid_Operator_IP' + '\n')
ipFile.close()
ipCidrRangeFile.close()
ipLookupResultFile.close()
Sample records for InputFile-1: 192.169.0.1 192.169.0.6 192.169.0.7
Sample records for InputFile-2:
192.169.0.1/32
192.169.0.6/16
255.255.255.0/32
255.255.255.0/16
192.169.0.7/32
Sample records for ResultFile.txt:
192.169.0.1 - Not_Valid_Operator_IP
192.169.0.6 - Valid_Operator_IP
192.169.0.7 - Not_Valid_Operator_IP