[关闭]
@Macux 2017-11-23T10:17:19.000000Z 字数 1982 阅读 1090

detect high anonymity IP of Install

工作


  1. import redis
  2. pool= redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
  3. r = redis.StrictRedis(connection_pool=pool)
  4. dict = {"16777216":'"16777471","DCH","AU","Australia","Queensland","Brisbane","Research Prefix for APNIC Labs"',
  5. "16781312":'"16785407","DCH","JP","Japan","Tokyo","Tokyo","I2TS Inc."',
  6. "16802923":'"16802923","VPN","JP","Japan","Shimane","Izumo","Energia Communications Inc."',
  7. "3758080000":'"3758088191","DCH","KR","Korea, Republic of","Seoul-teukbyeolsi","Seoul","Netropy Co. Ltd"',
  8. "3758092288":'"3758093311","DCH","HK","Hong Kong","Hong Kong (SAR)","Hong Kong","Metronet"',
  9. "3758094336":'"3758095359","DCH","HK","Hong Kong","Hong Kong (SAR)","Hong Kong","Capital Online Data Service HK Co Ltd"',
  10. "3758095872":'"3758096127","DCH","SG","Singapore","Singapore","Singapore","Marina Bay Sands Pte Ltd"',
  11. "3758096128":'"3758096383","DCH","AU","Australia","Queensland","Brisbane","APNIC Pty Ltd"'}
  12. # use a collection of hash to store providers data
  13. r.hmset('ip2location',dict)
  14. use a zset to index the max value of your ranges
  15. r.zadd('ip2location:index',16777471,"16777216",
  16. 16785407,"16781312",
  17. 16802923,"16802923",
  18. 3758088191,"3758080000",
  19. 3758093311,"3758092288",
  20. 3758095359,"3758094336",
  21. 3758096127,"3758095872",
  22. 3758096383,"3758096128")
  23. # 找出所有IP段右端点比当前IP大的最小的一个:默认是升序排列,所以取第一个即可
  24. ip = 3232235787
  25. # 如果这个IP段的左端点比当前IP小,即符合IP段条件,可以判断当前IP是否为高匿名IP
  26. # 4294967295是IP转十进制的最大值,故作为范围查询的区间
  27. leftDot = r.zrangebyscore('ip2location:index',min=ip,max=4294967295,start=0,num=1)
  28. if int(leftDot) <= ip:
  29. value = r.hget('ip2location',str(leftDot))
  30. if value.find('DCH') == -1:
  31. print 'Anonymity of current ip is high!'
  32. else:
  33. pass
  34. else:
  35. pass
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注