校验身份证号码(Google App Engine Python实现)

算法介绍:

身份证号码中的校验码是身份证号码的最后一位,是根据〖中华人民共和国国家标准 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)

 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 = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
 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 = 0
12         
13         for i in range(17):
14             checkNum = checkNum + int(identityNo[i])*self.checkFactor[i]
15         
16         self.checkResult = self.checkCode[checkNum % 11]
17         if checkResult == identityNo[17]:
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 = None
26         identityCheck = IdentityCheck()
27         if identityNo:
28             result = identityCheck.checkIdentityNo(identityNo)
29         
30         template_values ={ 'result' : result,
31                            'inputNo' : identityNo,
32                            'outputNo' : identityNo[0:-1] + 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()

2. 页面展示的HTML文件(main.html)

<html>  
    <body>  
          <form action="/sign" method="post">  
            Type Identity Number: <input type="text" name="id">  
            <input type="submit" value="Go">  
          </form> 
          
  {% if result %}
      {% ifequal inputNo outputNo %} 
          {{ result }} 
      {% else %}
          {{ result }} <br>
          Right identity number is {{ outputNo }}.
      {% endifequal %}
  {% endif %}
   </body>  
</html>

3. 最重要的app.yaml文件

application: sample-app
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: identitycheck.py

通过上面的3步已经完成了code的部分,接下来就可以本地部署测试,测试完成就可以发布到Google的服务器上去了。

本地部署有两种方法,一种是通过eclipse直接部署,在项目上右键选择Run->Pydev: Google App Run,完成之后就可以在浏览器中通过http://localhost:8080访问,另外一种方法是通过命令行去部署,使用下面的命令

/google_appengine/dev_appserver.py /root/workspace/identitycard/

#如果想清除历史的数据可以加带一个参数
/google_appengine/dev_appserver.py -c /root/workspace/identitycard/

 

posted @ 2012-08-03 14:08  DanielXLee  阅读(1021)  评论(1编辑  收藏  举报