简体   繁体   English

GAE WSGI应用程序URL映射

[英]GAE WSGI Application URL Mapping

I have an App Engine project structure setup as follows: 我有一个App Engine项目结构设置,如下所示:

    ProjectRoot 项目根目录
    • app.yaml app.yaml
    • index.yaml index.yaml
    • main.py main.py
    • static [directory] 静态[目录]
      • index.html index.html
    • app [directory] 应用[目录]
      • script1.py script1.py
      • script2.py script2.py

My app.yaml looks like this 我的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"

My main.py is simply the default 'Hello World' sample application: 我的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)

Now, the static html can be accessed as expected. 现在,可以按预期访问静态html。 The url mapping to the main.py script specified in app.yaml works and I know that the script is getting executed. URL映射到app.yaml中指定的main.py脚本有效,并且我知道该脚本正在执行。 The trouble I am having is with the URL mapping to be specified to WSGIApplication in main.py. 我遇到的麻烦是要在main.py中将URL映射指定为WSGIApplication。 I want to be able to access the application script using the url: localhost:808x/app/something I have already tried using the patterns: 我希望能够使用url访问应用程序脚本:localhost:808x / app /我已经尝试使用模式的东西:

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

None of the above patterns lead to the 'get' response handler being invoked (ie I don't get the 'Hello World' response). 以上任何一种模式都不会导致“ get”响应处理程序被调用(即,我没有得到“ Hello World”响应)。 I have tried gleaning what I am doing wrong from the documentation. 我尝试从文档中收集我做错的事情。 I think it all boils down to my only just coming to grips with regular expressions. 我认为这全归结为我只是对正则表达式有所了解。 Would someone possibly be able to point me to what pattern I need to map the application handler to? 有人可以指出我需要将应用程序处理程序映射到什么模式吗?

How about this pattern? 这种模式怎么样?

r'/app/.*'

If there is any regexp grouping, you need arguments for the view function. 如果有任何正则表达式分组,则需要view函数的参数。

Additionally, you need to add main() function in your main.py if you specify your script in the form like main.py . 另外,如果您以main.py之类的形式指定脚本,则需要在main.py中添加main()函数。 The main() function looks like: main()函数如下所示:

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

if __name__ == '__main__':
    main()

You can also use this form: 您也可以使用以下形式:

script: main.app

With the latter form, you don't need the main() function. 对于后一种形式,您不需要main()函数。

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

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