GAE WSGI应用程序URL映射

时间:2012-04-27 12:36:20

标签: google-app-engine

我有一个App Engine项目结构设置如下:

    ProjectRoot     
            
    • 的app.yaml
    •       
    • index.yaml中
    •       
    • main.py
    •       
    • 静态[目录]           
                      
      • 的index.html
      •           
            
    •       
    • app [目录]           
                      
      • 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。 url映射到app.yaml中指定的main.py脚本有效,我知道脚本正在执行。我遇到的麻烦是在main.py中将URL映射指定给WSGIApplication。我希望能够使用url:localhost:808x / app / something访问应用程序脚本 我已经尝试过使用这些模式:

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

上述模式都没有导致调用'get'响应处理程序(即我没有得到'Hello World'响应)。我已经尝试从文档中收集我做错了什么。我认为这一切归结为我唯一刚刚掌握正则表达式。有人可能能够指出我需要将应用程序处理程序映射到哪个模式吗?

1 个答案:

答案 0 :(得分: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()函数。

相关问题