2012-04-27 38 views
1

我有一個App Engine項目結構設置如下:GAE WSGI應用URL映射

    ProjectRoot
    • 的app.yaml
    • index.yaml中
    • main.py
    • 靜態[目錄]
      • index.html
    • 應用[目錄]
      • script1.py
      • script2.py

我的app.yaml看起來像這樣

application: appname 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: no 

handlers: 
- url: /(.*\.html) 
    mime_type: text/html 
    static_files: static/\1 
    upload: static/(.*\.html) 
    expiration: "1h" 

# application scripts 
- url: /app/(.+) 
    script: main.py 

# index files 
- url: /(.+)/ 
    static_files: static/\1/index.html 
    upload: static/(.+)/index.html 
    expiration: "15m" 

- url: /(.+) 
    static_files: static/\1/index.html 
    upload: static/(.+)/index.html 
    expiration: "15m" 

# site root 
- url:/
    static_files: static/index.html 
    upload: static/index.html 
    expiration: "15m" 

libraries: 
- name: webapp2 
    version: "2.5.1" 

我main.py只是默認的「Hello World」示例應用程序:

#!/usr/bin/env python 
import webapp2 

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     self.response.out.write('Hello world!') 

    #print("Executing script!") 
app = webapp2.WSGIApplication([(r'/app/(.*)', MainHandler)], 
           debug=True) 

現在,可以按預期訪問靜態html。映射到app.yaml中指定的main.py腳本的url工作,我知道該腳本正在執行。我遇到的麻煩是將URL映射指定爲main.py中的WSGIApplication。我希望能夠通過URL來訪問應用程序腳本:本地主機:808X /應用/東西 我已經使用模式的嘗試:

r'/app/(.*)' 
r'/(.*)' 
r'/' 
r'/app/' 

無上述模式導致「得到」響應處理器被調用(即我沒有得到'Hello World'響應)。我試圖從文檔中收集我做錯了什麼。我想這一切都歸結爲我只能正確表達正則表達式。有人可能能夠指出我需要什麼模式來映射應用程序處理程序?

回答

1

這種模式怎麼樣?

r'/app/.*' 

如果有任何正則表達式分組,您需要視圖函數的參數。

此外,如果您以main.py的形式指定腳本,則需要在main.py中添加main()函數。在main()函數如下:

from google.appengine.ext.webapp.util import run_wsgi_app 
... 
... 
def main(): 
    run_wsgi_app(app) 

if __name__ == '__main__': 
    main() 

您也可以使用這種形式:

script: main.app 

對於後一種形式,你不需要在main()函數。

+0

不,似乎沒有解決它。 – balajeerc 2012-04-27 12:58:51

+0

更新了我的答案。 – 2012-04-27 13:13:56

+0

謝謝,那就是訣竅! – balajeerc 2012-04-27 19:09:04