An ID verification interface [4]

Code:

#usr/bin/python
# -*- coding: utf-8 -*-
# Filename:idChecker.py

import cityZoneDB
import re

class idChecker():
    def __init__(self):
        self.db = cityZoneDB.CZDB()
        self.bigMonthes = (1, 3, 5, 7, 8, 10, 12)
        self.smallMonthes = (4, 6, 9, 11)
        self.weightList = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
        self.validationList = (1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2)

    # 样式是否合法
    def validation(self, idNumber):
        # 传参格式校验
        # 正则(一共18位, 前17位为数字最后以为要么是数字要么是X)
        idNumber = idNumber.upper()
        pattern = '^[0-9]{18}|[0-9]{17}[X]{1}$'
        if not re.search(pattern, idNumber, re.M):
            print '身份证号码:%s格式错误' % idNumber
            return False

        self.db.connect()
        # 地区代码校验
        zoneCode = self.checkZoneCode(idNumber[0:6])
        if not zoneCode:
            print '身份证号码:%s行政编码错误' % idNumber
            return False
        self.db.dbConnection.close()

        # 出生年月日校验(平润年大小月日期)
        birthday = self.checkBirthDay(idNumber[6:14])
        if not birthday:
            print '身份证号码:%s出生日期错误' % idNumber
            return False

        # 校验码校验
        checkCode = self.checkCode(idNumber)
        if not checkCode:
            print '身份证号码:%s验证码错误' % idNumber
            return False

        genderAndSerial = self.genderAndSerial(idNumber[14:17])
        addition = '是当时统计周期内该地区第%d派出所辖区内当天出生的第%d名%s性' % (genderAndSerial[0], genderAndSerial[1], '男' if genderAndSerial[2] else '女')
        print '身份证号码:%s合法,%s,%s,%s' % (idNumber, zoneCode, birthday, addition)
        return True


    # 行政编码校验
    def checkZoneCode(self, zoneCode):
        try:
            cityCode = (int(zoneCode))/100*100
            provinceCode = (int(zoneCode)/10000)*10000
            sql = 'select address_name from %s where address_id in (%d, %d, %d)' % (self.db.newtable, provinceCode, cityCode, int(zoneCode))
            # 执行SQL语句
            self.db.cursor.execute(sql)
            # 获取所有记录列表
            cityNames = self.db.cursor.fetchall()
            if len(cityNames) == 3:
                return '籍贯:'+cityNames[0][0]+cityNames[1][0]+cityNames[2][0]
            else:
                print '城市编号不存在'
                return False
        except Exception as e:
            print e.message
            print "Error: unable to fecth data"

    # 生日校验
    def checkBirthDay(self, birthday):
        year = int(birthday[0:4])
        month = int(birthday[4:6])
        day = int(birthday[6:8])
        isLeap = self.isLeapYear(year)
        if isLeap and (month == 2) and (day > 29):
            print '闰年出生日期不能超过2月29日'
            return False
        elif (not isLeap) and (month == 2) and (day>28):
            print '非闰年出生日期不能超过2月28日'
            return False
        elif (month in self.bigMonthes) and (day>31):
            print '大月出生日期不能超过31日'
            return False
        elif (month in self.smallMonthes) and (day>30):
            print '小月出生日期不能超过30日'
            return False
        elif not (month>0 and month<13):
            print '出生月份必须在1-12之间'
            return False
        elif not (day>0 and day<32):
            print '出生日期必须在1-31之间'
            return False
        else:
            return '出生日期:%d年%d月%d日' % (year,month,day)

    # 判断是否闰年
    def isLeapYear(self,year):
        if year % 172800 == 0:
            return True
        elif year % 3200 != 0 and year % 400 == 0:
            return True
        elif year % 100 != 0 and year % 4 == 0:
            return True
        else:
            return False

    # 派出所代码,出生序号,性别校验
    def genderAndSerial(self, gas):
        if int(gas[2]) % 2 == 0:
            return (int(gas[0:2]),int(gas[2])/2,0)
        else:
            return (int(gas[0:2]),(int(gas[2])+1)/2,1)

    # 验证身份证校验码
    def checkCode(self, idNumber):
        i = sum = 0
        while i < 17:
            sum += int(idNumber[i])*self.weightList[i]
            i += 1
        key = sum % 11
        code = self.validationList[key]
        if idNumber[-1] == str(code):
            return True
        else:
            return False


id = idChecker()
testNums = ['441225197608310342', '542224198002260419', '511529199111301159', '510108199204230658', '410211199307102667']
# 连接数据库
# id.db.connect()
# 进行校验
for num in testNums:
    id.validation(num)
# 关闭数据库连接
# id.db.dbConnection.close()

result:

/usr/bin/python2.7 /home/c80k2/PycharmProjects/spider/idChecker.py
数据库连接成功
身份证号码:441225197608310342合法,籍贯:广东省肇庆市封开县,出生日期:1976年8月31日,是当时统计周期内该地区第3派出所辖区内当天出生的第2名女性
数据库连接成功
身份证号码:542224198002260419合法,籍贯:西藏自治区山南地区桑日县,出生日期:1980年2月26日,是当时统计周期内该地区第4派出所辖区内当天出生的第1名男性
数据库连接成功
身份证号码:511529199111301159合法,籍贯:四川省宜宾市屏山县,出生日期:1991年11月30日,是当时统计周期内该地区第11派出所辖区内当天出生的第3名男性
数据库连接成功
身份证号码:510108199204230658合法,籍贯:四川省成都市成华区,出生日期:1992年4月23日,是当时统计周期内该地区第6派出所辖区内当天出生的第3名男性
数据库连接成功
身份证号码:410211199307102667合法,籍贯:河南省开封市郊区,出生日期:1993年7月10日,是当时统计周期内该地区第26派出所辖区内当天出生的第3名女性

Process finished with exit code 0

Test data source: The first five items of http://shenfenzheng.293.net/?_t_t_t=0.9766452528640315.

As you can see, the data matches. At the same time, the data used for testing is randomly combined by the program, which is also easy to implement. Algorithms for falsifying ID numbers should have come from programs like this before the nationwide internet a few years ago. Make it happen tomorrow.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325141784&siteId=291194637