通过IP地址判断客户端是网通的还是电信的。

使用一个纯文本的IP纯真数据库,用Python写了一个小程序。

核心代码:

  1. #!/usr/bin/env python
  2. #site www.jbxue.com
  3. #
  4. # -*- coding: utf-8 -*-
  5. from bisect import bisect
  6. _LIST1, _LIST2 = [], []
  7. _INIT = False
  8. ip2int = lambda ip_str: reduce(lambda a, b: (a << 8) + b, [int(i) for i in ip_str.split('.')])
  9. def _init():
  10. global _LIST, _INIT
  11. if not _INIT:
  12. for l in open('ipdata.txt', 'rb'):
  13. ip1, ip2 = l.split()[:2]
  14. addr = ' '.join(l.split()[2:])
  15. ip1, ip2 = ip2int(ip1), ip2int(ip2)
  16. _LIST1.append(ip1)
  17. _LIST2.append((ip1, ip2, addr))
  18. _INIT = True
  19. def ip_from(ip):
  20. _init()
  21. i = ip2int(ip)
  22. idx = bisect(_LIST1, i)
  23. assert(idx > 0)
  24. if len(_LIST1) <= idx:
  25. return u'unknown ip address %s' % ip
  26. else:
  27. frm, to ,addr = _LIST2[idx - 1]
  28. if frm <= i <= to:
  29. return addr
  30. else:
  31. return u'unknown ip address %s' % ip
  32. if __name__ == '__main__':
  33. print ip_from('115.238.54.106')
  34. print ip_from('220.181.29.160')
  35. print ip_from('115.238.54.107')
  36. print ip_from('8.8.8.8')

最新文章

  1. SharePoint 2013 Search 配置总结
  2. 学习iOS前我们需要知道的事情
  3. fastCGI与PHP-fpm
  4. 第一个android应用程序
  5. Windows 窗体启动和关闭的事件顺序
  6. Upgrading to MySQL 5.7---focusing on temporal types
  7. java_Cookie添加和删除
  8. DataReader 和 DataSet 的区别
  9. 基于MINA框架快速开发网络应用程序
  10. ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统
  11. 关于DataTable序列化的事儿
  12. Pair Project1:电梯控制程序
  13. java运行报错:nested exception is java.lang.NoSuchFieldError: INSTANCE,但使用@Test测试是好的
  14. shared_ptr(作为局部变量返回)
  15. Java高并发综合
  16. js异步原理与 Promise
  17. java 通过调用存储过程获取结果集
  18. java 基础 --final--008
  19. 聊聊flink的Async I/O
  20. 洛谷P1434 滑雪

热门文章

  1. bash中的快捷键使用
  2. 解决myeclipse不编译的方法
  3. android基础知识复习——RelativeLayout布局属性、背景、半透明设置(XML设置)
  4. daemon函数的原理及使用详解
  5. Caused by: org.apache.ibatis.reflection.ReflectionException我碰到的情况,原因不唯一
  6. 解决 java.lang.ClassNotFoundException配置文件出错的问题
  7. C# 中的&quot;yield&quot;与 &quot;yield break&quot;使用
  8. redis学习笔记——命令执行流程
  9. 算法笔记_090:蓝桥杯练习 7-1用宏求球的体积(Java)
  10. hibernate 关系映射之 双向外键关联一对一