以下代码实现了ip查询功能
处理程序
复制代码 代码如下:
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')
block_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)
生成IP
复制代码 代码如下:
#Generate 100 millon ip
import random
import time
def generateIpAdd(file,num):
ip=[]
file=open(file,'a+')
for i in range(num):
ipAdd='192.168.'+str(random.randint(0,255))+'.'+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)
ip查询
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】