简体   繁体   中英

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

I have a web site up and running with about 100 urls specified in my app.yaml file. Because app.yaml is limited to 100 urls, I am trying to figure out how to transfer the app.yaml specified routing instructions into main.py. I want modify (shorten) the app.yaml file to send all requests to main.py and then I want main.py to route the requests as app.yaml currently does. I have been experimenting with the three files below but can't get a response of "Hello from MainHandler in test1.py" if I request any url other than /test1. Why am I not getting a browser response of "Hello from MainHandler in test1.py" request a url of /xyz?

Test 1:

Request: /test1

Response: Hello from MainHandler in test1.py

Test 2:

Request: /example

Response: Hello from ExampleHandler in main.py

Test3:

Request: /xyz

Response: Hello MainHandler in main.py

Why doesn't Test3 above also result in a response of "Hello from MainHandler in test1.py" and how can I make this happen?

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"

The precedence hierarchy goes from app.yaml -> handlers specified in the program.

  • When you hit /test1 , app.yaml routes your request to test1.app (because it matches the /test1 route) which routes your request to test1.MainHandler .
  • When you hit /test3 , app.yaml routes your request to main.app (because it matches the .* route) which routes your request to main.MainHandler .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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