python生成二代有效身份证号及其核验有效性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python生成二代有效身份证号及其核验有效性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1798字,纯文字阅读大概需要3分钟。
内容图文
![python生成二代有效身份证号及其核验有效性](/upload/InfoBanner/zyjiaocheng/851/108c3b6ed7194977b141e07989a2fccb.jpg)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author simpelzhuo
import random,time
class IdCardGenerator():
""" 生成身份证号 """
def idCardRandomGeneratorRand(self):
""" 随机生成新的18为身份证号码 """
ARR = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
LAST = ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
t = time.localtime()[0]
x = '%02d%02d%02d%04d%02d%02d%03d' % (random.randint(10, 99), random.randint(1, 99), random.randint(1, 99), random.randint(t - 80, t - 18), random.randint(1, 12), random.randint(1, 28), random.randint(1, 999))
y = 0
for i in range(17):
y += int(x[i]) * ARR[i]
IDCard = '%s%s' % (x, LAST[y % 11])
return IDCard
def checkTrue(self,x1):
""" 验证身份证号码是否真实号码 """
ARR = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
LAST = ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
xlen = len(x1)
if xlen != 18 and xlen != 15:
return '身份证号码长度错误'
try:
if xlen == 18:
x2 = x1[6:14]
x3 = time.strptime(x2, '%Y%m%d')
if x2 < '19000101' or x3 > time.localtime():
return '时间错误,超过允许的时间范围'
else:
x2 = time.strptime(x1[6:12], '%y%m%d')
except Exception as e:
return '时间错误,非合法时间' + str(e)
if xlen == 18:
y = 0
for i in range(17):
y += int(x1[i]) * ARR[i]
if LAST[y % 11] != x1[-1].upper():
return '验证码错误'
return 'YES'
if __name__ == '__main__':
icg = IdCardGenerator()
idCardNO = icg.idCardRandomGeneratorRand()
print(idCardNO)
print(icg.checkTrue(idCardNO))
# t = time.localtime()[0]
# print('%02d'% random.randint(t - 80, t - 18))
内容总结
以上是互联网集市为您收集整理的python生成二代有效身份证号及其核验有效性全部内容,希望文章能够帮你解决python生成二代有效身份证号及其核验有效性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。