python实现ip查询示例

六月下雨了,天又下了雨。我已经很久没有见过如此下雨了,柔软而缠绵,就像你的眼睛,轻轻地刷着我寂寞的肩膀。闻一闻书本,站在窗前,对朋友说:早上好!

以下代码实现了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)

本文python实现ip查询示例到此结束。成功是别人失败时还在坚持。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战