Question

I'm trying google app engine with python27.

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

    - url: /hello
      script: helloworld.app

    - url: /.*
      script: main.app

helloworld.py and main.app have the same code from the offical document with little difference (response string).

import webapp2
class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!!!')
app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

My result: 1. "~", the response comes from "main.app". 2. "~/favicon.ico", the response comes from "favicon.ico". 3. "~/hello", the response is "404". 4. "~/something", the response is "404".

Sorry, to post this question, "~" for "http://localhost:8080".

Why 3 and 4 cannot be handled? Is there something wrong?

Was it helpful?

Solution

Try changing ('/', MainHandler) to (r'/.*', MainHandler) (the r just indicates it is a raw string). The problem is that you don't currently have any handlers for anything other than your root /, so requests with other parameters (such as http://localhost:8080/hello) have no matching handler and therefore it is unknown how to handle it. Changing the handler to /.* means that all requests (regardless of what comes after the root) should be routed to your MainHandler.

As for the app.yaml handlers and the handlers in your *.py file, think of the app.yaml as the high-level director. It gets a request and simply determines where to send it (am I getting a request for a static Javascript file? CSS? Picture? Or is this a request that should serve a page, etc.?). In the case such as yours above, you want it to serve a particular page when hitting any URL that is not /favicon (so /.*), so what it does is takes any request to /.* and routes it to main.app, which is in your main.py file (I'm ignoring helloworld.app for now, mainly because in your situation you don't necessarily need it).

It is then that the more granular handling happens - main.app receives the original request, and then it looks for a specific handler to execute the code. When we change your handlers to r'/.*', it matches anything that comes in (so /, /hello, /helloworld, etc.), and it executes the corresponding class MainHandler, in this case).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top