繁体   English   中英

GAE WSGI应用程序URL映射

[英]GAE WSGI Application URL Mapping

我有一个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。 URL映射到app.yaml中指定的main.py脚本有效,并且我知道该脚本正在执行。 我遇到的麻烦是要在main.py中将URL映射指定为WSGIApplication。 我希望能够使用url访问应用程序脚本:localhost:808x / app /我已经尝试使用模式的东西:

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

以上任何一种模式都不会导致“ get”响应处理程序被调用(即,我没有得到“ Hello World”响应)。 我尝试从文档中收集我做错的事情。 我认为这全归结为我只是对正则表达式有所了解。 有人可以指出我需要将应用程序处理程序映射到什么模式吗?

这种模式怎么样?

r'/app/.*'

如果有任何正则表达式分组,则需要view函数的参数。

另外,如果您以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()函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM