偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Google App Engine應(yīng)用示例:計算校驗位(Python)

開發(fā) 開發(fā)工具
本文提供了一個簡單的Python應(yīng)用并部署在Google App Engine上??偟膩碚f作者感覺GAE很好用,但在使用中文和保護上還存在一些問題。

剛好我以前學(xué)習(xí)Python的時候?qū)戇^一個計算身份證最后校驗位的小程序,我何不將其做成一個Web應(yīng)用呢,使用表單接受輸入,計算后將結(jié)果通過HTML頁面返回給用戶。使用GAE(Google App Engine)來發(fā)布這個小的Web應(yīng)用,如何融入框架呢?

相關(guān)閱讀:開始您的第一個Google App Engine應(yīng)用

下面是這個web應(yīng)用所有的代碼:

  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.  

這個程序中最關(guān)鍵的代碼就是main函數(shù)生成webapp.WSGIApplication實例這一句,這個類的構(gòu)造函數(shù)接受了兩個參數(shù),其中前面這個list類型的變量為不同的URL注冊了各自的handler,如果用戶輸入的是fuzhijie1985.appspot.com/,那么將觸發(fā)MainPage.get方法被執(zhí)行,這個方法生成了一個表單,而表單的action="/sign",因此提交時將觸發(fā)Guestbook.post方法被執(zhí)行,在這個方法內(nèi)將計算身份證的校驗位,然后返回給用戶一個HTML,內(nèi)容只有一個字符,那就是校驗位。另外需要注意的是Guestbook.get方法是如何從表單輸入框中獲得身份證號碼的,Python CGI的標(biāo)準(zhǔn)方法就是上面代碼那樣的。

我發(fā)現(xiàn)GAE真是太棒了,我不用費任何心思去管這個web應(yīng)用,它會一直在那兒為用戶服務(wù),用戶可以通過"

發(fā)現(xiàn)的幾個問題:

1、我開始很討厭Python語句塊的對齊。如果代碼有這樣的錯誤,上傳時是不會有任何錯誤的,打開鏈接時表單都看不到,GAE的程序似乎很不好調(diào)試。如果是本地的程序,對齊出現(xiàn)錯誤,運行時Python解釋器會指明錯誤。

2、不能使用中文。我發(fā)現(xiàn)不僅HTML的內(nèi)容中不能有中文,連中文注釋都不能使用,否則下場和上面一樣,連表單都看不到。

3、這個程序沒有任何保護措施,所以輸入的身份證號碼如果少于17就會拋出異常,這是因為for循環(huán)要執(zhí)行17次,當(dāng)少于輸入的數(shù)字少于17時,比如輸入16位時,No[17]就不存在,訪問它就要拋異常了。瀏覽器中可以看到如下內(nèi)容:

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上實現(xiàn)文檔存儲和搜索
  2. 手把手教你在Google App Engine上運行PHP
  3. 開始您的第一個Google App Engine應(yīng)用
  4. 教你如何在Google App Engine上運行PHP
  5. 使用Java開發(fā)Google APP Engine初試
責(zé)任編輯:yangsai 來源: 163博客
相關(guān)推薦

2009-04-14 11:01:33

GoogleApp EngineGroovy

2012-08-01 14:12:45

IBMdW

2009-10-16 09:08:59

App Engine

2009-09-02 11:34:09

Google App

2009-08-07 13:53:14

App Engine

2009-10-14 10:11:39

App Engine

2010-02-01 09:21:49

GroovyGoogle App Gaelyk

2009-04-13 15:48:54

Google AppJavaSun

2009-05-22 14:52:33

App Engine免費配額

2009-04-08 16:47:11

GoogleApp EngineJava

2009-08-11 11:23:41

什么是GAEGoogle App

2009-04-09 08:54:07

App EnginegoogleJava

2011-09-06 14:53:01

Google App

2009-07-03 09:03:01

Google App 故障

2009-05-13 09:20:12

Google App 應(yīng)用收費

2009-02-16 09:11:42

Google App SDKGQL

2011-09-15 10:29:13

架構(gòu)

2012-06-29 09:33:55

Google云平臺

2013-07-30 12:29:19

Google App Google技術(shù)Engine

2009-09-04 09:41:34

Google App
點贊
收藏

51CTO技術(shù)棧公眾號