1 / 60

網路應用程式開發

網路應用程式開發. 使用 Google App Engine. Introduction. What is the could computing? 是透過網路將龐大 的運算處理程式 自動 分拆成無數個較小的 子程式 ,再由多部伺 服器所組成的龐大系 統搜尋、運算分析之 後將處理結果回傳給使用者。. Google : App Engine. 網路平台,讓開發者可自行建立網路應用程式於 google 平台中。 提供: 500MB of storage up to 5 million page views a month

shelby
Download Presentation

網路應用程式開發

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 網路應用程式開發 使用Google App Engine

  2. Introduction • What is the could computing? • 是透過網路將龐大 的運算處理程式自動 分拆成無數個較小的 子程式,再由多部伺 服器所組成的龐大系 統搜尋、運算分析之 後將處理結果回傳給使用者。

  3. Google : App Engine • 網路平台,讓開發者可自行建立網路應用程式於google平台中。 • 提供: • 500MB of storage • up to 5 million page views a month • 10 applications per developer account • Limit: • Language: Python、Java • web applications

  4. Google : App Engine的特性 • 物件導向資料庫:類似很大的Eecel檔案,這個檔案有很多的欄位和很多的列數。 • 無法使用Join:不是關聯式資料庫,所以沒有JOIN的概念. • 容錯能力優越 • 處理附載平衡

  5. The Sandbox Environment • App Engine applications cannot: • write to the filesystem. Applications must use the App Engine datastore for storing persistent data. Reading from the filesystem is allowed, and all application files uploaded with the application are available. (Files uploaded as "static" files are not kept on the filesystem.) • open a socket or access another host directly. An application can use the App Engine URL fetch service to make HTTP and HTTPS requests to other hosts on ports 80 and 443, respectively. • spawn a sub-process or thread. A web request to an application must be handled in a single process within a few seconds. Processes that take a very long time to respond are terminated to avoid overloading the web server. • make other kinds of system calls, such as signals.

  6. WebApp Framework • The WebApp Framework is the built in framework for handling requests • WSGI compatible framework • Uses Request Handlers classes to serve pages • Receives a WebOb request object • Response writes buffers to memory, returns output when handler exits

  7. Bigtable • Google File System(GFS):為google自行開發的分散式文件系統,多欄位的設計,不大需要特別的正規化。 • MapReduce:平行資料處理模型,將要計算檔案分解為Map 和Reduce。

  8. MapReduce

  9. Ex:haddoop • Data: Stream of keys and values

  10. 安裝googleappengine • Download the SDK • You must have Python 2.5 installed on your computer to use the App Engine SDK • http://code.google.com/p/googleappengine • Windows Installer • Mac App Engine Launcher • Zip Archive

  11. 建置第一個GAE專案 • Setp 1

  12. 建置第一個GAE專案(Cont.) • Step 2

  13. 建置第一個GAE專案(Cont.) • Step 3 • PS:模擬本地端伺服器為dev_appserver.py

  14. 建置第一個GAE專案(Cont.) • App.yaml : google配置文件 • main.py : 網頁開始執行入口程式 • Index.yaml : 提高網頁程式執行效能。

  15. 建置第一個GAE專案(Cont.) • 建立helloworld.py • 修改app.yaml內script為helloworld.py

  16. 建置第一個GAE專案(Cont.) • 執行browse,恭喜你完成第一個GAE檔案

  17. Django 簡介 • 純粹的 Python package。 • MTV 程式設計架構。 • 物件化的資料庫模型(Model)。 • 可延伸的模板系統(Template)。 • 軟體所需要的功能(View)。 • 內建的管理介面 (admin app) 以及必要的支援功能 (使用者/群組、行程等)。 • 內建開發用的 web server。 • 官方建議「絕對不要」用內建的 web server 提供任何非開發用途的服務。 • 已知可佈署於 Apache1/2, lighttpd, twisted 三種 web server,支援 mod_python, SCGI, FCGI 三種模式。

  18. 安裝Django(setp1) • 前置作業 • 安裝Python2.6.5 • 安裝 Google App Engine • 下載Django1.0.2 • 將下載下來的資料夾中『django』整個複製到C:\Python25\Lib\site-packages • 將解壓縮後的Django資料夾放置在C:\底下 • 進入命令列模式(開始→執行→cmd),加入Django資料夾底下後輸入: python setup.py install

  19. 安裝Django(setp2) • 建立Django項目目錄 • 在環境變數中加入C:\Python26\Scripts • 建立django專案 django-admin.py startproject專案名稱 完成後,可以看見專案目錄下產生四個檔案 • __init__.py • manage.py:可以用來啟動django伺服器,運行所開發的網頁程式。 • settings.py:這是Django的配置文件。 • urls.py:網址映射處理文件 • 只需複製settings.pyandurls.py兩個檔案

  20. 安裝Django(setp3) • 修改app.yaml

  21. 安裝Django(setp4) • 建立main.py

  22. 安裝Django(setp5) GAE所不允許的模組 設定模板的路徑為目前所在的檔案路徑+templates GAE所不允許的模組

  23. 利用Django編寫Helloworld • 建立模板(Templates) • 檔名:helloworld.py • 內容:<b>{{messae}}</b>#表示使用一個message變數在模板中 *jango中{{}}括號表示使用一個變數 *{%%}表示使用一段程式碼 2

  24. GAE網站管理系統(step1)

  25. GAE網站管理系統(step2)

  26. GAE網站管理系統(step3)

  27. GAE網站管理系統(step4)

  28. GAE網站管理系統(step5)

  29. 實際的網站 • 建立好的網站 • Google語法查詢

  30. Querying for Data • Google provides two methods for queryingdata • GQL, a SQL-like query language • We also have a query interface,which you can read more about at: • http://code.google.com/appengine/docs

  31. EX:Fibonacci series • 模板 • <html> • <head> • <title></title> • </head> • <body> • <b>{{num1}}</b><br/> • <b>{{num2}}</b><br/> • <b>{{num3}}</b><br/> • <b>{{num4}}</b><br/> • <b>{{num5}}</b><br/> • <b>{{num6}}</b><br/> • </body> • </html>

  32. #coding=utf-8 • from django.shortcuts import render_to_response • def index(request): • ans=fib(100) • return render_to_response('show.html' • , {'num1':ans • }) • def fib(n): # write Fibonacci series up to n • "Print a Fibonacci series up to n" • msg="" • a, b = 0, 1 • while b < n: • msg+=(str(b)+",") • a, b = b, a+b • return msg

  33. Google appengine基礎指令介紹 • Datastore Model Class語法 • ratingproperty使用者提供的內容評等,介於 0 和 100 之間的整數。 • Call put() on the object to add it to thedatastore

  34. GQL query

  35. Django常用指令介紹 Django線上手冊

  36. 會員註冊頁面(基本資料庫操作) • HTML部分(模板) • Container.html(父模板) • Register.html(子模板) 網頁實際運作時,子模板的內容會代入父模板的特定語法區塊中。 • 父模板只需編輯一次即可重複使用

  37. 會員註冊頁面(基本資料庫操作) • 模型架構(model.py) • model.py • 控制架構(register.py) • Register.py

  38. *GQL語法 • count(limit) 傳回此次查詢所擷取的結果數量。 • get() 執行查詢,然後傳回第一個結果,或者 None (若查詢沒有傳回任何結果)。 • get() 隱含 1 的「限制」,並覆寫 GQL 查詢的 LIMIT 子句 (若有使用的話)。最多只會從資料存放區擷取 1 筆結果。 • fetch(limit, offset=0) 執行查詢,然後傳回結果。 • Limit 和 offset 引數控制從資料存放區擷取的結果數量,以及 fetch() 方法傳回的結果數量。

  39. *GQL語法(Cont.) • 搜尋方式 • query = Song.gql("WHERE composer = 'Lennon, John'") • className.Put(): 將資料INSERT一筆新的資料到資料庫 • 正則表達式(Regular Expression) • re.search(pattern, string) • Pattern :正則表達式 • String: 要處理的字串 • 回傳:符合正則表達式的字串位置

  40. 資料庫的新增、刪除、修改、選取(留言板製作)資料庫的新增、刪除、修改、選取(留言板製作) • 功能詳述: • 資料模板 • model.py加入Guesbook • Accountid:用來記錄最後留言的人 • Title:這篇留言的標題 • Content:留言的內容 • Data:留言的建立時間 • 新增、刪除、修改、選取() • Comment.py

  41. 資料庫的新增、刪除、修改、選取(留言板製作)資料庫的新增、刪除、修改、選取(留言板製作) • Update 語法:db.put(欲修改的物件) • insert語法:instanceName.put() • 使用GQL語法和直接呼叫get()函式所傳回的資料型態是不一樣的 • Gql傳回gqlquery物件=m.guesBook.gql(data(key)) • Get()傳回guestBook資料庫物件 • Delete語法:m.db.delete(欲刪除的資料物件) • PS:urlpath是定義在webconfig.py的變數,用來儲存目前的網址。

  42. 資料庫的新增、刪除、修改、選取(留言板製作)資料庫的新增、刪除、修改、選取(留言板製作) • 設定顯示模板 • Listcomment.html

  43. 一對多資料庫關係實作 • 建立model.py(資料庫模型) • class GuesBook2(db.Model): user = db.ReferenceProperty(UserData) title = db.StringProperty() content = db.TextProperty() date = db.DateTimeProperty(auto_now_add=True) *db.ReferenceProperty資料型態:user檔案儲存 為一個參考 (reference)。儲存的為一個唯一的Key值。 ReferenceProperty資料型別式由Google自動產生唯一的key值

  44. 上傳超過1MB的資料到DataStore • 建立模型 Blobproperty型態:以二進制編碼來儲存檔案

  45. 上傳超過1MB的資料到DataStore Parent Pic-3 Pic-1 Pic-2 ……………… Pic-3-1 Pic-3-2 Pic-3-n Pic-1-1 Pic-1-2 Pic-1-n Pic-2-1 Pic-2-2 Pic-2-n child

  46. 交易(transaction) • A transaction is a datastore operation or a set of datastore operations that either succeed completely, or fail completely. If the transaction succeeds, then all of its intended effects are applied to the datastore. If the transaction fails, then none of the effects are applied. • 全部執行成功 • 全部執行失敗 • 使用時機,有相關性連續性的檔案結構

  47. Entity Group(資料群) • 相關的資料都在Data Group附近儲存. • Google僅允許Entity Group進行transaction的動作 UserData instance Gbook Gbook Gbook Gbook Gbook Gbook Gbook Gbook

  48. 實作資料交易 • 函式:db.run_in_transaction(funcObj, **kwd) • funcObj:傳入一個function物件,也就是一個函式名稱 • Kwd:允許傳入一連串的變數,這一連串的變數其實就是要傳入funcObj所表示的函式中。因此funcObj=n, Kwd= n 個對應的變數。 • Transactionmodel.py

  49. 圖片的儲存與圖像處理 • 安裝python imaging library(PIL) • 測試是否安裝成功 • 開啟IDLE (Python GUI) • 鍵入 import Image 成功則是安裝成功。

More Related