The following code implements the ip address query function
Handler
Copy codeThe Code is as follows:
Import OS, time
Def getip (filepath ):
Ip2city = {}
File = open (filepath, 'R ')
Lines = file. readlines ()
File. close ()
For line in lines:
Ip = line. split ('') [0]
City = line. split ('') [1]
Haship = hashm (ip)
If haship in ip2city:
Pass
Else:
Ip2city [haship] = city
Print ('hash done! ')
Return ip2city
Def hashm (ip ):
Iplist = ip. split ('.')
Ip = int (iplist [0]) * 4 + int (iplist [1]) * 2 + int (iplist [2])
Return ip
Def getcityfromip (filepath, ipandcity ):
Outputstr = []
For file in OS. listdir (filepath ):
File_handler = open (filepath + '\' + file, 'R ')
Line = file_handler.readline ()
While line:
Ip = hashm (line. rstrip ())
If ip in ipandcity:
Outputstr. append (line. rstrip () + ''+ ipandcity [ip])
Line = file_handler.readline ()
File_handler.close ()
Outfile_handler = open (filepath + '\ '{file.split('.'{0}}'_out.txt', 'a + ')
Outfile_handler.writelines (outputstr)
Outfile_handler.close ()
Print(file.split('.'{}0}}'_out.txt '+ 'done! ')
Def splitfile (filepath ):
File = open (filepath, 'R ')
Blocking _size = 8000000
Filecount = 1
Temp = []
Count = 0
Line = file. readline ()
While line or temp:
If count = block_size:
Wfile = open ('d: \ ipfile \ file_'{str(filecount}'.txt ', 'a + ')
Wfile. writelines (temp)
Temp = []
Count = 0
Wfile. close ()
Filecount + = 1
Print ('split '+ str (filecount) + 'done! ')
Else:
Count + = 1
Temp. append (line)
Line = file. readline ()
File. close ()
Return OS. path. join ('d: \ '+ 'ipfile ')
If _ name _ = '_ main __':
Start = time. clock ()
Filepath = 'd: \ ip.txt'
Ippath = 'd: \ citys.txt'
Ip2city = getip (ippath)
Splitfilepath = splitfile (filepath)
Getcityfromip ('d: \ '+ 'ipfile', ip2city)
End = time. clock ()
Print (end-start)
Generate IP
Copy codeThe Code is as follows:
# Generate 100 millon ip
Import random
Import time
Def generateIpAdd (file, num ):
Ip = []
File = open (file, 'a + ')
For I in range (num ):
IpAdd = '1970. 192. '+ str (random. randint (168) +'. '+ str (random. randint (0,255 ))
Ip. append (ipAdd + '\ n ')
File. writelines (ip)
File. close ()
If _ name __= = '_ main __':
Start = time. clock ()
For I in range (10000 ):
GenerateIpAdd ('d: \ ip.txt ', 10000)
End = time. clock ()
Print (end-start)