YAML file url and script in GAE python

后端 未结 2 810
别那么骄傲
别那么骄傲 2021-01-26 19:57

I\'m using Python 2.7 in Google App Engine and can\'t seem to get my app.yaml file set up right.

My goal is so that if I go to http://localhost/carlos/ I ge

相关标签:
2条回答
  • 2021-01-26 20:35

    I was able to determine the solution and figured I'd post it for anyone out there.

    In my carlos.py file I needed to replace:

    app = webapp2.WSGIApplication([('/', MainHandler)],
                                  debug=True)
    

    with

    app = webapp2.WSGIApplication([('/carlos/', MainHandler)],
                                  debug=True)
    

    It appears that the first argument for the WSGIApplication is referring to the TOTAL path from your root web address as opposed to the INCREMENTAL path from which it was originally directed.

    I'm selecting this answer over what was provided by Littm because I'd like to keep using WSGI

    0 讨论(0)
  • 2021-01-26 20:56

    It worked with the following modifications:

    1 - Replacing "carlos.app" by "carlos.py" and "main.app" by "main.py" in the yaml file.

    2 - Add a slash ("/") after " /carlos " in the file "carlos.py".

    3 - Add the following part of code in the end of each python files (carlos.py and main.py)

    def main():
        app.run()
    

    Here is an example of modified files:

    app.yaml :

    application: myapp
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: no
    
    handlers:
    - url: /carlos/.*
      script: carlos.py
    
    - url: .*
      script: main.py
    

    carlos.py : import webapp2

    class MainHandler(webapp2.RequestHandler):
      def get(self):
        self.response.out.write("Hello, Carlos!")
    
    app = webapp2.WSGIApplication([('/carlos/', MainHandler)],
                              debug=True)
    
    def main():
        app.run()
    

    main.py :

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

    You can try navigating to:

    localhost:8080/carlos/ and localhost:8080/ to see the results

    Hope it helps ;)

    0 讨论(0)
提交回复
热议问题