当前位置: 移动技术网 > IT编程>脚本编程>Python > python实现ip查询示例

python实现ip查询示例

2019年03月31日  | 移动技术网IT编程  | 我要评论

与我长跑十年,鹰獒,386旅

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网