Python Google App Engine: Call specific method from yaml file?
I am new to database programming wit开发者_开发知识库h Google App Engine and am programming in Python. I was wondering if I am allowed to have one Python file with several request handler classes, each of which has get and post methods. I know that the yaml file allows me to specify which scripts are run with specific urls, like the example below:
handlers:
- url: /.*
script: helloworld.py
How would I tell it to run a specific method that is in one of the classes in the .py file? Is that even possible/allowed? Do I need to separate the different request handler classes into different python files? My understanding of databases is rather shallow at the moment, so I could be making no sense.
Thanks.
I was wondering if I am allowed to have one Python file with several request handler classes, each of which has get and post methods.
Sure! That app.yaml
just transfers control to helloworld.py
, which will run the main
function defined in that file -- and that function typically sets up a WSGI app which dispatches appropriately, depending on the URL, to the right handler class. For example, look at the sample code here, very early on in the tutorial:
application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I'm not copying the import
statements and class definitions, because they don't matter: this is an example of how a single .py
file dispatches to various handler classes (two in this case).
This doesn't mean the yaml file lets you call any method whatsoever, of course: rather, it hands control to a .py
file, whose main
is responsible for all that follows (and e.g. with the webapp
mini-framework that comes with App Engine, it will always be get
or post
method [[or put
, delete
, ..., etc, if you also support those -- few do unless they're being especially RESTful;-)]] being called depending on the exact HTTP method and URL in the incoming request.
精彩评论