【问题标题】:GAE WSGI Application URL MappingGAE WSGI 应用程序 URL 映射
【发布时间】:2012-05-08 04:58:41
【问题描述】:

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

    项目根目录
    • 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 有效,我知道该脚本正在执行。我遇到的问题是要在 main.py 中指定到 WSGIApplication 的 URL 映射。我希望能够使用 url 访问应用程序脚本:localhost:808x/app/something 我已经尝试过使用这些模式:

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

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

【问题讨论】:

    标签: google-app-engine


    【解决方案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() 函数。

    【讨论】:

    • 不,这似乎无法解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2012-05-23
    • 2013-06-29
    • 1970-01-01
    • 2018-01-26
    • 2014-09-15
    • 2013-09-09
    相关资源
    最近更新 更多