一個生成隨機身份證號碼的接口

明天快到了python

代碼:算法

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

import cityZoneDB
import idChecker
import re
import random
import time

class idMaker():
    def __init__(self):
        self.db = cityZoneDB.CZDB()
        self.ic = idChecker.idChecker()
        self.now = time.localtime()
        self.startYear = 1900
        # self.startTime = time.mktime(time.strptime(self.startYear,"%Y-%M-%d"))
        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 setZoneCodeCount(self):
        try:
            sql = "select id from %s order by id desc limit 1" % self.db.newtable
            self.db.connect()
            self.db.cursor.execute(sql)
            res = self.db.cursor.fetchall()
            if len(res) == 1:
                self.zoneCodeCount = int(res[0][0])
                return True
            else:
                print '獲取數量失敗'
                return False
            self.db.dbConnection.close()
        except Exception as e:
            print e.message
            print "Error: unable to fecth data"

    # 隨機獲取一個身份證號碼
    def getRandomIdCode(self):
        zoneCodeId = self.getRandomZoneCode()
        birthday = self.getRandomBirthday()
        policeStationCode = self.getRandomPoliceStationCode()
        genderAndSerial = self.getRandomGenderAndSerial()
        elements = zoneCodeId+birthday+policeStationCode+genderAndSerial
        checkCode = self.getCheckCode(elements)
        idNumber = elements+checkCode
        if zoneCodeId and birthday and policeStationCode and genderAndSerial and checkCode:
            print '已生成隨機身份證:%s,即將進行校驗' % idNumber
            self.ic.validation(idNumber)
            return True
        else:
            print '生成隨機身份證失敗'
            return False

    # 隨機獲取一個行政編碼
    def getRandomZoneCode(self):
        try:
            id = random.randrange(1, self.zoneCodeCount, 1)
            sql = "select address_id from %s where id=%d" % (self.db.newtable, id)
            self.db.connect()
            self.db.cursor.execute(sql)
            res = self.db.cursor.fetchall()
            if len(res) == 1:
                if self.getZoneName(int(res[0][0])):
                    return str(res[0][0])
            else:
                print '獲取數量失敗'
                return False
            self.db.dbConnection.close()
        except Exception as e:
            print e.message
            print "Error: unable to fecth data"

    # 隨機生成一個合法的出生日期
    def getRandomBirthday(self):
        nowYear = int(time.strftime("%Y", self.now))-1
        randYear = random.randrange(self.startYear, int(nowYear))
        randMonth = random.randrange(1, 12)
        if self.ic.isLeapYear(randYear) and randMonth == 2:
            randDate = random.randrange(1, 29)
        elif randMonth == 2:
            randDate = random.randrange(1, 28)
        elif randMonth in self.bigMonthes:
            randDate = random.randrange(1, 31)
        elif randMonth in self.smallMonthes:
            randDate = random.randrange(1, 30)
        #格式轉換
        randYear = str(randYear)
        randMonth = str(randMonth) if randMonth > 10 else '0'+str(randMonth)
        randDate = str(randDate) if randDate > 10 else '0'+str(randDate)
        birthday = '%s%s%s' % (randYear, randMonth, randDate)

        return birthday

    # 隨機生成一個派出所號
    def getRandomPoliceStationCode(self):
        randCode = random.randrange(1, 100)
        return str(randCode) if randCode > 10 else '0'+str(randCode)

    # 隨機生成一個性別和序列號
    def getRandomGenderAndSerial(self):
        return str(random.randrange(0, 9))

    # 經過算法生成校驗碼
    def getCheckCode(self, elements):
        i = sum = 0
        while i < 17:
            sum += int(elements[i]) * self.weightList[i]
            i += 1
        key = sum % 11
        return str(self.validationList[key])

    # 獲取行政名稱
    def getZoneName(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:
                # print '籍貫:'+cityNames[0][0]+cityNames[1][0]+cityNames[2][0]
                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"


im = idMaker()
im.setZoneCodeCount()
im.getRandomIdCode()

執行結果sql

/usr/bin/python2.7 /home/c80k2/PycharmProjects/spider/.idea/idMaker.py
數據庫鏈接成功
數據庫鏈接成功
已生成隨機身份證:340322198705035182,即將進行校驗
數據庫鏈接成功
身份證號碼:340322198705035182合法,籍貫:安徽省蚌埠市五河縣,出生日期:1987年5月3日,是當時統計週期內該地區第51派出所轄區內當天出生的第4名女性

Process finished with exit code 0
相關文章
相關標籤/搜索