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中的try和finally和with方法
May 05 Python
Python外星人入侵游戏编程完整版
Mar 30 Python
Python 处理图片像素点的实例
Jan 08 Python
Python threading的使用方法解析
Aug 28 Python
使用TensorFlow-Slim进行图像分类的实现
Dec 31 Python
Python-openCV读RGB通道图实例
Jan 17 Python
Python django框架开发发布会签到系统(web开发)
Feb 12 Python
Python 字符串处理特殊空格\xc2\xa0\t\n Non-breaking space
Feb 23 Python
Django获取model中的字段名和字段的verbose_name方式
May 19 Python
python实例化对象的具体方法
Jun 17 Python
如何实现一个python函数装饰器(Decorator)
Oct 12 Python
python利用xlsxwriter模块 操作 Excel
Oct 14 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+lottery.js实现九宫格抽奖功能
2019/07/21 PHP
实例:用 JavaScript 来操作字符串(一些字符串函数)
2007/02/15 Javascript
List Installed Hot Fixes
2007/06/12 Javascript
JavaScript为对象原型prototype添加属性的两种方式
2010/08/01 Javascript
javascript实现信息的显示和隐藏如注册页面
2013/12/03 Javascript
JavaScript实现的简单幂函数实例
2015/04/17 Javascript
基于jQuery实现Ajax验证用户名是否存在实例
2016/03/30 Javascript
jquery之别踩白块游戏的简单实现
2016/07/25 Javascript
基于vue2.0+vuex的日期选择组件功能实现
2017/03/13 Javascript
js调用刷新界面的几种方式
2017/05/03 Javascript
Vue实现点击后文字变色切换方法
2018/02/11 Javascript
浅谈Webpack多页应用HMR卡住问题
2019/04/24 Javascript
[07:54]DOTA2 MV《我的动力鞋》 ImbaTV 出品
2014/11/21 DOTA
python字典序问题实例
2014/09/26 Python
用Python编写脚本使IE实现代理上网的教程
2015/04/23 Python
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
2016/05/27 Python
python绘制简单彩虹图
2018/11/19 Python
详解python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案
2018/12/02 Python
利用nohup来开启python文件的方法
2019/01/14 Python
django之状态保持-使用redis存储session的例子
2019/07/28 Python
Django url,从一个页面调到另个页面的方法
2019/08/21 Python
Python制作词云图代码实例
2019/09/09 Python
python 二维矩阵转三维矩阵示例
2019/11/30 Python
Python 实现向word(docx)中输出
2020/02/13 Python
在pycharm中debug 实时查看数据操作(交互式)
2020/06/09 Python
分享一枚pycharm激活码适用所有pycharm版本我的pycharm2020.2.3激活成功
2020/11/20 Python
HTML5 Canvas 实现圆形进度条并显示数字百分比效果示例
2017/08/18 HTML / CSS
Sofft鞋官网:世界知名鞋类品牌
2017/03/28 全球购物
团员个人的自我评价
2013/12/02 职场文书
地球一小时宣传标语
2014/06/24 职场文书
法人委托书范本
2014/09/15 职场文书
2014年移动公司工作总结
2014/12/08 职场文书
文员岗位职责范本
2015/04/16 职场文书
2015暑期社会实践调查报告
2015/07/14 职场文书
用Python写一个简易版弹球游戏
2021/04/13 Python
如何理解Vue简单状态管理之store模式
2021/05/15 Vue.js