【问题标题】:YAML file url and script in GAE pythonGAE python中的YAML文件url和脚本
【发布时间】:2012-08-31 22:01:48
【问题描述】:

我在 Google App Engine 中使用 Python 2.7,但似乎无法正确设置我的 app.yaml 文件。

我的目标是,如果我转到 http://localhost/carlos/,我会得到一个已执行的 carlos.py

这是我的目录结构:

app\
   \app.yaml
   \main.py
   \carlos.py

这是我当前的 app.yaml 文件:

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /carlos/.*
  script: carlos.app

- url: .*
  script: main.app

我的 carlos.py 文件是:

import webapp2

class MainHandler(webapp2.RequestHandler):
  def get(self):
    self.response.out.write("Hello, Carlos!")

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

但是,我现在得到的只是 404 Not Found 错误。有什么想法吗?

【问题讨论】:

    标签: python google-app-engine yaml


    【解决方案1】:

    它进行了以下修改:

    1 - 在 yaml 文件中将“carlos.app”替换为“carlos.py”,将“main.app”替换为“main.py”。

    2 - 在文件“carlos.py”中的“/carlos”之后添加一个斜杠(“/”)。

    3 - 在每个 python 文件(carlos.py 和 main.py)的末尾添加以下部分代码

    def main():
        app.run()
    

    以下是修改文件的示例:

    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: 导入 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()
    

    ma​​in.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()
    

    您可以尝试导航到:

    localhost:8080/carlos/ 和 localhost:8080/ 查看结果

    希望对你有帮助;)

    【讨论】:

      【解决方案2】:

      我能够确定解决方案,并认为我会将其发布给那里的任何人。

      在我需要替换的 carlos.py 文件中:

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

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

      WSGIApplication 的第一个参数似乎是指从您的根网址开始的 TOTAL 路径,而不是最初指向它的 INCREMENTAL 路径。

      我选择这个答案而不是 Littm 提供的答案,因为我想继续使用 WSGI

      【讨论】:

        猜你喜欢
        • 2014-02-19
        • 1970-01-01
        • 2011-12-24
        • 2021-01-03
        • 1970-01-01
        • 2013-02-28
        • 1970-01-01
        • 2013-11-16
        • 2023-03-31
        相关资源
        最近更新 更多