校验身份证号码(Google App Engine Python实现)
<div id="cnblogs_post_body">算法介绍:身份证号码中的校验码是身份证号码的最后一位,是根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,根据精密的计算公式计算出来的,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码,最后一位是数字校验码。
校验身份证可以分为下面几步:
1. 将前面的身份证号码17位数分别乘以不同的系数, 从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
2. 将这17位数字和系数相乘的结果相加
3. 用加出来和除以11,看余数是多少?
4. 余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字, 其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2
5. 如果通过计算得到的余数和身份证最后一位一样, 则说明身份证号码是正确的
Python实现:
这里使用eclipse作为开发工具,在这之前需要安装Pydev插件,首先新建一个Pydev Googel App Engine Project
1. Python script文件(identitycheck.py)
<div class="cnblogs_code"> 1 import os 2 from google.appengine.ext import webapp 3 from google.appengine.ext.webapp.util import run_wsgi_app 4 from google.appengine.ext.webapp import template 5 6 class IdentityCheck(): 7 checkFactor = ] 8 checkCode = { 0 : '1',1 : '0',2 : 'x',3 : '9',4 : '8',5 : '7', 9 6 : '6',7 : '5',8 : '4',9 : '3',10 : '2'}10 def checkIdentityNo(self,identityNo):11 checkNum = 012 13 for i in range(17):14 checkNum = checkNum + int(identityNo)*self.checkFactor15 16 self.checkResult = self.checkCode]17 if checkResult == identityNo]:18 return "This identity card number(" + identityNo + ") is valid."19 else:20 return "This identity card number(" + identityNo + ") is invalid."21 22 class checkResult(webapp.RequestHandler):23 def post(self):24 identityNo = self.request.get('id')25 result = None26 identityCheck = IdentityCheck()27 if identityNo:28 result = identityCheck.checkIdentityNo(identityNo)29 30 template_values ={ 'result' : result,31 'inputNo' : identityNo,32 'outputNo' : identityNo + str(identityCheck.checkResult)}33 path = os.path.join(os.path.dirname(__file__),'main.html')34 self.response.out.write(template.render(path, template_values))35 36 class MainPage(webapp.RequestHandler):37 def get(self):38 path = os.path.join(os.path.dirname(__file__),'main.html')39 self.response.out.write(template.render(path, None))40 41 42 application = webapp.WSGIApplication([('/', MainPage),43 ('/sign', checkResult)], debug=True)44 45 46 def main():47 run_wsgi_app(application)48 49 if __name__ == "__main__":50 main()
页:
[1]