我有一个网站正在运行,在我的 app.yaml 文件中指定了大约 100 个 url。因为app.yaml被限制为100个url,所以我想弄清楚如何将app.yaml指定的路由指令传输到main.py中。我想修改(缩短)app.yaml 文件以将所有请求发送到 main.py,然后我希望 main.py 像 app.yaml 当前那样路由请求。我一直在试验下面的三个文件,但如果我请求除/test1 以外的任何 url,则无法收到“Hello from MainHandler in test1.py”的响应。为什么我没有收到“Hello from MainHandler in test1.py”的浏览器响应请求/xyz 的 URL?
测试 1:
请求:/test1
响应:来自 test1.py 中 MainHandler 的问候
测试 2:
请求:/示例
响应:来自 main.py 中的 ExampleHandler 的问候
测试 3:
请求:/xyz
响应:main.py 中的 Hello MainHandler
为什么上面的 Test3 没有产生“Hello from MainHandler in test1.py”的响应,我该如何实现?
主要.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)
测试1.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
应用程序.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 -> 程序中指定的处理程序开始。
- 当您点击
/test1
时,app.yaml
将您的请求路由到test1.app
(因为它匹配/test1
路由)将您的请求路由到test1.MainHandler
。 - 当您点击
/test3
时,app.yaml
将您的请求路由到main.app
(因为它匹配.*
路由)将您的请求路由到main.MainHandler
。
关于python - Q : google app engine app. yaml 如何处理 main.py 中的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38417523/