import sys,re

 ip_1_stat = {}
source_fp = open("ip_region","r")
p = re.compile("(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})")
for line in source_fp:
line = line.strip()
list = line.split('|') start = p.match(list[0].strip())
end = p.match(list[1].strip()[:-3])
region = list[2].strip() if start and end:
key = start.group(1)
if key not in ip_1_stat:
ip_1_stat[key] = {"line":{}}
if line not in ip_1_stat[key]["line"]:
ip_1_stat[key]["line"][line] = {"s2":int(start.group(2)), "s3":int(start.group(3)), "s4":int(start.group(4)), "e2":int(end.group(2)), "e3":int(end.group(3)), "e4":int(end.group(4)), "prov":region}
source_fp.close() #ip index
index = sys.argv[1] for line in sys.stdin:
line = line.strip()
list = line.split('\t') ip = list[index].strip()
#ip = list[2].strip()
match = p.match(ip)
i1 = match.group(1)
i2 = int(match.group(2))
i3 = int(match.group(3))
i4 = int(match.group(4))
prov = "NotFound" key = i1
if key not in ip_1_stat:
continue flag = 0
#ergodic
for l in ip_1_stat[key]["line"]:
#start
if i2 > ip_1_stat[key]["line"][l]["s2"]:
flag = 1
elif i2 == ip_1_stat[key]["line"][l]["s2"]:
if i3 > ip_1_stat[key]["line"][l]["s3"]:
flag = 1
elif i3 == ip_1_stat[key]["line"][l]["s3"]:
if i4 >= ip_1_stat[key]["line"][l]["s4"]:
flag = 1
#flag
if flag == 0:
continue #end
if i2 < ip_1_stat[key]["line"][l]["e2"]:
flag = ip_1_stat[key]["line"][l]["prov"]
elif i2 == ip_1_stat[key]["line"][l]["e2"]:
if i3 < ip_1_stat[key]["line"][l]["e3"]:
flag = ip_1_stat[key]["line"][l]["prov"]
elif i3 == ip_1_stat[key]["line"][l]["e3"]:
if i4 <= ip_1_stat[key]["line"][l]["e4"]:
flag = ip_1_stat[key]["line"][l]["prov"]
if flag != 1:
break print "%s\t%s" % (line, flag)

IP解析的更多相关文章

  1. IP工具类-自己动手做个ip解析器

    IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:

  2. dubbo注册服务IP解析异常及IP解析源码分析

    在使用dubbo注册服务时会遇到IP解析错误导致无法正常访问. 比如: 本机设置的IP为172.16.11.111, 但实际解析出来的是180.20.174.11 这样就导致这个Service永远也无 ...

  3. 动态IP解析

    本文介绍两种方便获取主机动态IP的方式(DDNS,IP报告网页),并给出相应的代码实现. shell脚本获取本机IP,执行上传操作和更新DNS操作.定期执行通过crontab或者systemd等服务. ...

  4. github开源的一些ip解析 ,运营商信息,经纬度 地址 后续开发使用

    https://github.com/wzhe06/ipdatabase  ip解析 https://github.com/flyaction/ipdatabase 比较新 https://githu ...

  5. 万网云解析全面升级开放,支持海外IP解析!

    基于万网过去18年来的专业域名解析服务经验,万网云解析新版实现了承载超过300万域名的全面升级,它是万网DNS域名解析系统的全新升级,目前已正式发布上线,详见万网首页:http://www.net.c ...

  6. 开发一个简单的ip解析webservice接口,并用springmvc生成客户端调用

    1.创建webservice工程,这次先采用jax-ws框架,下次再尝试jax-rs(restful) 2.写个实现ip解析的类,接收传入的ip,并返回解析信息 3.Myeclipse——>Ne ...

  7. Hive UDF IP解析(二):使用geoip2数据库自定义UDF

    开发中经常会碰到将IP转为地域的问题,所以以下记录Hive中自定义UDF来解析IP. 使用到的地域库位maxmind公司的geoIP2数据库,分为免费版GeoLite2-City.mmdb和收费版Ge ...

  8. 更新TP-LINK路由器的外网IP到花生壳动态IP解析

    ------------------------------------------------------------------------------- 以下内容可能还是存在问题,等之后有时间再 ...

  9. DNS寻址以及IP解析

    域名解析从右向左 DNS寻址: 1 客户端发送查询请求,在本地计算机缓存查询,若没有找到,就会将请求发送给dns服务器 2 先发送给本地的dns服务器,现在自己的区域内查找,若找到,根据此记录进行查询 ...

  10. IP解析计算机名称

    #-*- coding: UTF-8 -*- import subprocess,sys,threading reload(sys) sys.setdefaultencoding('utf-8') d ...

随机推荐

  1. NOIP模拟 poke - 贪心

    题目大意: 给一副牌(\(n \le 1000000, a_i \le 1000000\)),问最多能打出多少对对子(x, x)或者顺子(x, x + 1, x + 2). 题目分析: 感觉跟斗地主很 ...

  2. 用Canvas画一个刮刮乐

    Canvas 通过 JavaScript 来绘制 2D图形.Canvas 是逐像素进行渲染的.开发者可以通过javascript脚本实现任意绘图.Canvas元素是HTML5的一部分,允许脚本语言动态 ...

  3. ElasticSearch:分析器

    ElasticSearch入门 第七篇:分析器 这是ElasticSearch 2.4 版本系列的第七篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch El ...

  4. hadoop 3.x Replication与Availability不一致

    看下面的文字前先确保你的Replication值不大于你设置的虚拟机数量 如图,显示的副本数为3,但是实际可用的只有一台机器,查看了下hadoop003,hadoop004两台机器,果然没有存储数据, ...

  5. jeesuite分布式框架环境搭建

    一.简述 这是菜鸟走向开源的第一步.开源项目jeesuite:http://git.oschina.net/vakinge/jeesuite-libs jeesuite是托管在码云上的开源项目,是一个 ...

  6. Android中自定义View和自定义动画

    Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...

  7. xml报错(dtd):The markup declarations contained or pointed to by the document type declaration must be well-formed

    文件后缀为.xml里如下一行报错“The markup declarations contained or pointed to by the document type declaration mu ...

  8. Session Redis Nginx

    Session + Redis + Nginx 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我就简单的介绍一下. Session:在计算机 ...

  9. this解析

    定时器中的this function Aaa() { var _this = this; //解决关键 this.a = 12; //但凡被定时器调的函数,this必然是window,所以,show里 ...

  10. android 内存优化 性能优化

    最近做一个android 的应用程序 总是出现内存高 和cpu高的问题困扰了好多天. 下面为自己从网上总结的和自己找到的问题. 1. WebView  控件: 使用了 WebView 控件一定要注意清 ...