python实现ip查询示例

yipeiwu_com5年前Python基础

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

相关文章

Python3批量移动指定文件到指定文件夹方法示例

Python3批量移动指定文件到指定文件夹方法示例

引言 某人需求:以某excel中姓名信息为名建立一系列文件夹,分别将四个文件夹中与人名对应的文件汇总到该人名对应的文件夹中,共近200人,手工处理费时费力。 需求分解: 从exc...

Python3 伪装浏览器的方法示例

Python3 伪装浏览器的方法示例

一、伪装浏览器 对于一些需要登录的网站,如果不是从浏览器发出的请求,则得不到响应。所以,我们需要将爬虫程序发出的请求伪装成浏览器正规军。 具体实现:自定义网页请求报头。 二、使用Fid...

Python 如何提高元组的可读性

这篇文章主要介绍了Python 如何提高元组的可读性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 假设学生系统中数据为固定格式:(名...

总结用Pdb库调试Python的方式及常用的命令

用Pdb调试有多种方式 使用 Pdb调试 Python的程序的方式主要是下面的三种!下面逐一介绍 命令行加-m参数 命令行启动目标程序,加上-m参数,这样调用 testPdb.py的...

python在linux中输出带颜色的文字的方法

python在linux中输出带颜色的文字的方法

在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中。而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱...