現在 Google App Engine 在個作業系統平台都可以開發使用
而本次使用 Linux ubuntu 來當做範例
首先我們需要使用Google的帳號來創建一個專案
 |
在搜尋列搜尋 GAE console |
 |
登入後前往我的控制台 |
 |
建立雲端的專案 |
 |
專案名稱隨意取,專案ID須符合他的格式且不重複,後面上傳程式與雲端網址都會用到專案ID |
專案建立完成後記下專案ID帳號申請的這個步驟就算完成了
再來我們就要開始建設我們的編譯環境與第一個雲端程式
步驟開始前記得先更新你的作業系統一下
Linux現在都內建Python所以不用另外安裝
我們需要去 Google 的網站下載 Python 的 GAE SDK 開發套件
傳送門
依照自己的作業系統選擇你要的版本
我們這裡使用的是Linux的版本
下載解壓縮後我選擇把檔案放在我的家目錄讓我以後用它比較方便
 |
檔案下載下來後解壓縮 |
 |
把解壓縮完的資料夾移到家目錄 |
再來就能開始創建我們的雲端程式了
 |
創建我們的專案資料夾(update)與專案(Hello_world)的資料夾 |
其最基本的的雲端程式會有兩個檔案 暫定名稱.py 與 app.yaml
Google的範例
傳送門
py 檔的範例
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
app.yaml 的範例
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.application
其 application 就是輸入你剛剛創建的專案ID
script 後面的名稱需與py的檔名一樣
下面是本次的範例步驟
 |
這裡使用 vim 來編輯 python 檔 |
 |
Python 檔內容 |
 |
這裡使用 vim 來編輯 app.yaml 檔案 |
 |
application 要輸入剛剛申請的專案ID script 後面的檔名要跟py檔的檔名一樣 |
兩個檔案都創建完後能選擇單機測試或是直接上傳到雲端讓其他人看到你的網站
 |
利用 SDK 的套件 dev_appserver.py 上傳專案本機測試 |
 |
執行後可用瀏覽器在網址列輸入 http://localhost:8080 測試結果 |
 |
Hello, World! |
 |
你也可以選擇真正的放上網路 讓大家都能看到你的作品 |
 |
第一次上傳會需要輸入 Email 跟 password 來認證 |
 |
上傳完後就能連線到 專案ID.appspot.com 看結果囉 |
因老師要求附上 eclipse 的開發環境教學所以有需求的人可以參考一下
eclipse 這套軟體有在做開發的應該都不陌生
其強大的擴充功能讓我們要在上面做 GAE 的開發也很簡單
在 eclipse 上面用 python 做 GAE 的開發需要另外安裝 Pydev 這個套件