python实现ip查询示例


Posted in Python onMarch 26, 2014

以下代码实现了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 相关文章推荐
浅谈Python中函数的参数传递
Jun 21 Python
简单掌握Python的Collections模块中counter结构的用法
Jul 07 Python
python中 * 的用法详解
Jul 10 Python
Python可变参数会自动填充前面的默认同名参数实例
Nov 18 Python
Python笔记之观察者模式
Nov 20 Python
python列表返回重复数据的下标
Feb 10 Python
PIL包中Image模块的convert()函数的具体使用
Feb 26 Python
Python+Appium实现自动化测试的使用步骤
Mar 24 Python
Ubuntu中配置TensorFlow使用环境的方法
Apr 21 Python
pytorch中 gpu与gpu、gpu与cpu 在load时相互转化操作
May 25 Python
python等待10秒执行下一命令的方法
Jul 19 Python
Python通过len函数返回对象长度
Oct 22 Python
python fabric实现远程操作和部署示例
Mar 25 #Python
python基础教程之数字处理(math)模块详解
Mar 25 #Python
python操作摄像头截图实现远程监控的例子
Mar 25 #Python
python基础教程之字典操作详解
Mar 25 #Python
python基础教程之元组操作使用详解
Mar 25 #Python
python基础教程之简单入门说明(变量和控制语言使用方法)
Mar 25 #Python
python使用循环实现批量创建文件夹示例
Mar 25 #Python
You might like
PHP stristr() 函数(不区分大小写的字符串查找)
2010/06/03 PHP
PHP用SAX解析XML的实现代码与问题分析
2011/08/22 PHP
PHP中文件缓存转内存缓存的方法
2011/12/06 PHP
微信公众平台接口开发入门示例
2014/12/24 PHP
总结PHP中DateTime的常用方法
2016/08/11 PHP
PHP耦合设计模式实例分析
2018/08/08 PHP
JS动态修改表格cellPadding和cellSpacing的方法
2015/03/31 Javascript
Jquery时间轴特效(三种不同类型)
2015/11/02 Javascript
浅谈jquery高级方法描述与应用
2016/10/04 Javascript
一句jQuery代码实现返回顶部效果(简单实用)
2016/12/28 Javascript
Angular 开发学习之Angular CLI的安装使用
2017/12/31 Javascript
vue 根据数组中某一项的值进行排序的方法
2018/08/30 Javascript
高性能js数组去重(12种方法,史上最全)
2019/12/21 Javascript
[01:00:30]完美世界DOTA2联赛循环赛 Inki vs Matador BO2第二场 10.31
2020/11/02 DOTA
Python在Windows和在Linux下调用动态链接库的教程
2015/08/18 Python
python列表的增删改查实例代码
2018/01/30 Python
用Eclipse写python程序
2018/02/10 Python
Python比较2个时间大小的实现方法
2018/04/10 Python
Python中循环引用(import)失败的解决方法
2018/04/22 Python
Pycharm配置远程调试的方法步骤
2018/12/17 Python
Python自定义一个类实现字典dict功能的方法
2019/01/19 Python
Python字典对象实现原理详解
2019/07/01 Python
春节到了 教你使用python来抢票回家
2020/01/06 Python
分享全球十款超强HTML5开发工具
2014/05/14 HTML / CSS
HTML5自定义视频播放器源码
2020/01/06 HTML / CSS
Manuka Doctor美国官网:麦卢卡蜂蜜和蜂毒护肤
2016/12/25 全球购物
英国豪华家具和经典家居饰品购物网站:OKA
2020/06/05 全球购物
护理专科毕业推荐信
2013/11/10 职场文书
应届生会计求职信
2013/11/11 职场文书
医学生自荐信
2013/12/03 职场文书
关于责任的演讲稿
2014/05/20 职场文书
党员四风自我剖析材料思想汇报
2014/09/13 职场文书
法人代表证明书格式
2014/10/01 职场文书
写给父母的感谢信
2015/01/22 职场文书
2015年音乐教师个人工作总结
2015/05/20 职场文书
工程主管竞聘书
2015/09/15 职场文书