Google App Engine应用示例:计算校验位(Python)

开发 开发工具
本文提供了一个简单的Python应用并部署在Google App Engine上。总的来说作者感觉GAE很好用,但在使用中文和保护上还存在一些问题。

刚好我以前学习Python的时候写过一个计算身份证最后校验位的小程序,我何不将其做成一个Web应用呢,使用表单接受输入,计算后将结果通过HTML页面返回给用户。使用GAE(Google App Engine)来发布这个小的Web应用,如何融入框架呢?

相关阅读:开始您的第一个Google App Engine应用

下面是这个web应用所有的代码:

  1. import cgi  
  2. import wsgiref.handlers  
  3.  
  4. from google.appengine.api import users  
  5. from google.appengine.ext import webapp  
  6.  
  7. Wi = [791058421637,9105842]  
  8. IndexTable = {  
  9.   0 : '1',  
  10.   1 : '0',  
  11.   2 : 'x',  
  12.   3 : '9',  
  13.   4 : '8',  
  14.   5 : '7',  
  15.   6 : '6',  
  16.   7 : '5',  
  17.   8 : '4',  
  18.   9 : '3',  
  19.   10 : '2' 
  20.     }  
  21.  
  22. class Cal():  
  23.     def calculate(self, identity):  
  24.   No = []  
  25.   sum = 0 
  26.   No = list(identity)  
  27.  
  28.   for i in range(17):  
  29. sum = sum + (int(No[i]) * Wi[i])  
  30.  
  31.   Index = sum % 11 
  32.  
  33.   return IndexTable[Index]  
  34.  
  35. class MainPage(webapp.RequestHandler):  
  36.     def get(self):  
  37.   self.response.out.write("""  
  38. < html>  
  39.     < body>  
  40.   < form action="/sign" method="post">  
  41. Your card number: < input type="text" name="id">  
  42. < input type="submit" value="Got it">  
  43.   < /form>  
  44.     < /body>  
  45. < /html>""")  
  46.  
  47. class Guestbook(webapp.RequestHandler):  
  48.     def post(self):  
  49.   form = cgi.FieldStorage()  
  50.  
  51.   myid = form['id'].value  
  52.  
  53.   cal = Cal();  
  54.  
  55.   result = cal.calculate(myid);  
  56.  
  57.   self.response.out.write('< html>< body>')  
  58.   self.response.out.write(result)  
  59.   self.response.out.write('< /body>< /html>')  
  60.  
  61. def main():  
  62.  
  63.     application = webapp.WSGIApplication(  
  64.  [('/', MainPage),  
  65.   ('/sign', Guestbook)],  
  66.  debug=True)  
  67.  
  68.     wsgiref.handlers.CGIHandler().run(application)  
  69.  
  70. if __name__ == "__main__":  
  71.     main()  
  72.  

这个程序中最关键的代码就是main函数生成webapp.WSGIApplication实例这一句,这个类的构造函数接受了两个参数,其中前面这个list类型的变量为不同的URL注册了各自的handler,如果用户输入的是fuzhijie1985.appspot.com/,那么将触发MainPage.get方法被执行,这个方法生成了一个表单,而表单的action="/sign",因此提交时将触发Guestbook.post方法被执行,在这个方法内将计算身份证的校验位,然后返回给用户一个HTML,内容只有一个字符,那就是校验位。另外需要注意的是Guestbook.get方法是如何从表单输入框中获得身份证号码的,Python CGI的标准方法就是上面代码那样的。

我发现GAE真是太棒了,我不用费任何心思去管这个web应用,它会一直在那儿为用户服务,用户可以通过"http://fuzhijie1985.appspot.com/"使用这个服务,这个应用或许与Google永存吧(Google似乎只提供一个月的试用期,呵呵,一个月后我再看看它还在不在吧)。

发现的几个问题:

1、我开始很讨厌Python语句块的对齐。如果代码有这样的错误,上传时是不会有任何错误的,打开链接时表单都看不到,GAE的程序似乎很不好调试。如果是本地的程序,对齐出现错误,运行时Python解释器会指明错误。

2、不能使用中文。我发现不仅HTML的内容中不能有中文,连中文注释都不能使用,否则下场和上面一样,连表单都看不到。

3、这个程序没有任何保护措施,所以输入的身份证号码如果少于17就会抛出异常,这是因为for循环要执行17次,当少于输入的数字少于17时,比如输入16位时,No[17]就不存在,访问它就要抛异常了。浏览器中可以看到如下内容:

Traceback (most recent call last):  File "/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 509, in __call__    handler.post(*groups)  File "/base/data/home/apps/fuzhijie1985/2.335469091362125330/hello.py", line 57, in post    result = cal.calculate(myid);  File "/base/data/home/apps/fuzhijie1985/2.335469091362125330/hello.py", line 31, in calculate    sum = sum + (int(No[i]) * Wi[i])IndexError: list index out of range

【编辑推荐】

  1. 在Google Java App Engine上实现文档存储和搜索
  2. 手把手教你在Google App Engine上运行PHP
  3. 开始您的第一个Google App Engine应用
  4. 教你如何在Google App Engine上运行PHP
  5. 使用Java开发Google APP Engine初试
责任编辑:yangsai 来源: 163博客
相关推荐

2009-04-14 11:01:33

GoogleApp EngineGroovy

2012-08-01 14:12:45

IBMdW

2009-09-02 11:34:09

Google App

2009-10-16 09:08:59

App Engine

2009-08-07 13:53:14

App Engine

2009-10-14 10:11:39

App Engine

2009-04-09 08:54:07

App EnginegoogleJava

2011-09-06 14:53:01

Google App

2009-04-08 16:47:11

GoogleApp EngineJava

2009-05-22 14:52:33

App Engine免费配额

2010-02-01 09:21:49

GroovyGoogle App Gaelyk

2009-04-13 15:48:54

Google AppJavaSun

2009-08-11 11:23:41

什么是GAEGoogle App

2009-05-13 09:20:12

Google App 应用收费

2009-07-03 09:03:01

Google App 故障

2011-09-15 10:29:13

架构

2013-07-30 12:29:19

Google App Google技术Engine

2009-02-16 09:11:42

Google App SDKGQL

2009-09-04 09:41:34

Google App

2012-06-29 09:33:55

Google云平台
点赞
收藏

51CTO技术栈公众号