Python 随机生成有效手机号码及身份证

时间:2021-09-27 02:32:10

  中国那么大,人那么多,几乎人手一部手机。手机号码已经作为各大互联网站的注册账户。同样,身份证更是如此。以下是生成有效手机号码和身份证号。
  身份证需要下载districtcode.txt这个文件:http://files.cnblogs.com/files/yicaifeitian/districtcode.rar

 BASE_DIR = os.path.dirname(os.path.dirname(__file__))
 DC_PATH = BASE_DIR  + "districtcode.txt"

 # 随机生成手机号码
 def createPhone():
     prelist=["]
     ") for i in range(8))

 # 随机生成身份证号
 def getdistrictcode():
     with open(DC_PATH) as file:
         data = file.read()
         districtlist = data.split('\n')
     for node in districtlist:
     #print node
         if node[10:11] != ' ':
             state = node[10:].strip()
         if node[10:11]==' 'and node[12:13]!=' ':
             city = node[12:].strip()
         if node[10:11] == ' 'and node[12:13]==' ':
             district = node[14:].strip()
             code = node[0:6]
             codelist.append({"state":state,"city":city,"district":district,"code":code})

 def gennerator():
     global codelist
     codelist = []
     if not codelist:
         getdistrictcode()
     id = codelist[random.randint(0,len(codelist))]['code'] #地区项
     id = id + str(random.randint(1930,2013)) #年份项
     da = date.today()+timedelta(days=random.randint(1,366)) #月份和日期项
     id = id + da.strftime('%m%d')
     id = id+ str(random.randint(100,300))#,顺序号简单处理 

     i = 0
     count = 0
     weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] #权重项
     checkcode ={'} #校验码映射
     for i in range(0,len(id)):
         count = count +int(id[i])*weight[i]
         id = id + checkcode[str(count%11)] #算出校验码
         return id

 print createPhone()
 print gennerator()