首页 > 安全资讯 >

用Python实现查询手机号码归属地实用代码

10-09-24

用Python实现手机号码归属查询代码Python 2.6.2 (r262:71600, Aug 4 2009, 09:26:47)[GCC 3.4.6 20060404 (Red Hat 3.4.6-10)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> from Phon

用Python实现手机号码归属查询代码

  1. Python 2.6.2 (r262:71600, Aug 4 2009, 09:26:47)
  2. [GCC 3.4.6 20060404 (Red Hat 3.4.6-10)] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> from PhoneRegion import getRegion,loadRegion
  5. >>> numsdb = loadRegion()
  6. >>> print getRegion(1234123,numsdb)
  7. 未知,未知
  8. >>> print getRegion(15213492123,numsdb)
  9. 未知,未知
  10. >>> print getRegion(13213492123,numsdb)
  11. 河南濮阳,联通130卡
  12. >>> print getRegion(13513492123,numsdb)
  13. 河北沧州,移动全球通卡
  14. #!/usr/bin/env python
  15. # -*- coding=utf-8 -*-
  16. import re
  17. import sys
  18. import gzip
  19. numberRe = re.compile(^1[0-9]{10}$)
  20. numsDbRe = re.compile((^1[0-9]{6}) (.*) (.*))
  21. def getRegion(number,numsDb):
  22.     if not numberRe.search(number):
  23.         return 未知,未知
  24.     if not numsDb:
  25.         return 未知,未知
  26.     k = number[0:3]
  27.     v = number[3:7]
  28.     if not k in numsDb:
  29.         return 未知,未知
  30.     else:
  31.         region = numsDb[k].get(v,(未知,未知))
  32.         return ,.join(region)
  33. def loadRegion():
  34.     RegionDb = {}
  35.     try:
  36.         for line in gzip.open(numsdb.tar.gz).readlines():
  37.             line = line.strip()
  38.             m = numsDbRe.search(line)
  39.             if m:
  40.                 nums,area,card = m.groups()
  41.                 k = nums[0:3]
  42.                 v = nums[3:7]
  43.                 if k in RegionDb:
  44.                     RegionDb[k][v] = (area,card)
  45.                 else:
  46.                     RegionDb[k]={}
  47.                     RegionDb[k][v] = (area,card)
  48.         return RegionDb
  49.     except Exception,e:
  50.         print e
  51.         return {}
  52. if __name__ == __main__:
  53.     numsDb = loadRegion()
  54.     try:
  55.         number = sys.argv[1]
  56.         print number,getRegion(number,numsDb)
  57.     except:
  58.         pass

相关文章
最新文章
热点推荐