【发布时间】:2023-03-11 04:09:01
【问题描述】:
我有一个网站启动并运行,在我的 app.yaml 文件中指定了大约 100 个网址。因为 app.yaml 限制为 100 个 url,所以我想弄清楚如何将 app.yaml 指定的路由指令传输到 main.py 中。我想修改(缩短)app.yaml 文件以将所有请求发送到 main.py,然后我希望 main.py 像 app.yaml 当前那样路由请求。我一直在试验下面的三个文件,但如果我请求除 /test1 之外的任何 url,则无法得到“来自 test1.py 中 MainHandler 的 Hello”的响应。为什么我没有收到“Hello from MainHandler in test1.py”的浏览器响应请求 /xyz 的 url?
测试 1:
请求:/test1
响应:来自 test1.py 中 MainHandler 的您好
测试 2:
请求:/示例
响应:来自 main.py 中的 ExampleHandler 的您好
测试3:
请求:/xyz
响应:你好 main.py 中的 MainHandler
为什么上面的 Test3 也不会导致“Hello from MainHandler in test1.py”的响应,我怎样才能做到这一点?
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"
【问题讨论】:
标签: python url-routing app.yaml