Google AppEngine: Python 2.7: How to split request handler up into several files?

467 views Asked by At

I have an application in which I would like to move some of the request handlers into separate files. I've reduced the problem to this simple app which demonstrates it.

If I browse the main page, e.g.:

http://localhost:12082/  

I see

'Hello World...' 

displayed

if I try to go to the init page, e.g.:

http://localhost:12082/init/

I see

'404 Not Found'
The resource could not be found.

and in the log I see this: (init2 message is never logged, the helper method is never called.)

What am I doing wrong? Thank you for any clues

log contents:


...
INFO     2014-01-15 20:58:23,384 admin_server.py:117] Starting admin server at: http://localhost:8005

INFO     2014-01-16 04:58:34,398 initter.py:6] init1

INFO     2014-01-16 04:58:34,398 initter.py:17] init3

INFO     2014-01-15 20:58:34,421 module.py:617] default: "GET / HTTP/1.1" 200 14
INFO     2014-01-15 20:58:34,473 module.py:617] default: "GET / HTTP/1.1" 200 14
INFO     2014-01-15 20:58:39,555 module.py:617] default: "GET /init/ HTTP/1.1" 404 154

app.yaml:

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: helloworld.application

helloworld.py


import webapp2
import initter

class MainPage(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write('Hello World...')

application = webapp2.WSGIApplication([
  ('/', MainPage),
  ('/init', initter.LoadPeople),
  ], debug=True)

initter.py


import webapp2
import logging

logging.getLogger().setLevel(logging.DEBUG)

logging.info("init1")

class LoadPeople(webapp2.RequestHandler):
  def get(self):
    logging.info("init2")
    names = [ 'Joe', 'Jill', 'George', 'John', 'Dave' ]
    for name in names:
       logging.debug("name is %s", str(name))

logging.info("init3")
1

There are 1 answers

3
Prahlad Yeri On BEST ANSWER

http handler expressions could be tricky. There is a difference between '/init' and '/init/' (with an extra slash at the end). Try changing your WSGI app initialization to this:

application = webapp2.WSGIApplication([
  ('/', MainPage),
  ('/init*', initter.LoadPeople),
  ], debug=True)

This will also take care of any query-strings you provide. Example: "....localhost/init?someKey=someValue"