繁体   English   中英

问:谷歌应用引擎app.yaml如何处理main.py中的网址?

[英]Q: google app engine app.yaml how to handle urls from within main.py?

我有一个正在运行的网站,并在我的app.yaml文件中指定了大约100个URL。 由于app.yaml的网址限制为100个,因此我试图找出如何将app.yaml指定的路由指令传输到main.py中。 我想修改(缩短)app.yaml文件以将所有请求发送到main.py,然后希望main.py像app.yaml当前一样将请求路由。 我一直在尝试下面的三个文件,但是如果我请求除/ test1以外的任何URL,则无法得到“来自test1.py中MainHandler的Hello响应”。 为什么我没有得到浏览器响应“来自test1.py中MainHandler的Hello”,请求/ xyz的URL?

测试1:

要求:/ test1

响应:您好来自test1.py中的MainHandler

测试2:

要求:/ example

响应:来自main.py中的ExampleHandler的你好

TEST3:

要求:/ xyz

响应:main.py中的Hello MainHandler

为什么上面的Test3还没有收到“来自MainHandler的test1.py中的Hello响应”,我该怎么做呢?

main.py:

#!/usr/bin/env python
import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello MainHandler in main.py<br>')
        import test1
        test1.app

class ExampleHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello from ExampleHandler in main.py<br>')

app = webapp2.WSGIApplication([
    ('/example', ExampleHandler),
    (r'.*', MainHandler)],
    debug=True)

test1.py:

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello from MainHandler in test1.py')

app = webapp2.WSGIApplication([(r'.*', MainHandler)], debug=True)

if __name__ == "__main__":
    app

app.yaml中:

application: test-for-rr
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

 - url: /test1
   script: test1.app

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"

优先级层次结构来自程序中指定的app.yaml->处理程序。

  • 当您点击/test1app.yaml将您的请求路由到test1.app (因为它与/test1路由匹配),从而将您的请求路由到test1.MainHandler
  • 当您点击/test3app.yaml将您的请求路由到main.app (因为它匹配.*路由),从而将您的请求路由到main.MainHandler

暂无
暂无

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

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