【问题标题】:Update Google App Engine to Python 2.7将 Google App Engine 更新到 Python 2.7
【发布时间】:2013-11-08 00:30:41
【问题描述】:

我之前尝试过通过更改来更新我的应用程序

运行时:python27,线程安全:true,脚本:main.app"

它确实有效,并且在 python 2.7 上运行,但我猜它没有正常运行,因为当我转到 url http://dhsenviro.appspot.com 时我的 index.html 没有显示。它现在在 2.5 上运行(因为我想保持它)。 robots.txt 为空。如何将其更新到 2.7 还是应该将其更新到 3.x?

app.yaml:

application: dhsenviro
version: 1
runtime: python
api_version: 1

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|psd|swf))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css|psd|swf))

- url: /robots.txt
  static_files: robots.txt
  upload: robots.txt 

- url: /.*
  script: main.py

main.py:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

我不想上传我的 index.html,但我确信它工作正常。

编辑:

main.py:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

class application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)

def main ():

if __name__ == '__main__':
  main ()

正确的 APP.YAML:

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

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|psd|swf))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css|psd|swf))

- url: /robots.txt
  static_files: robots.txt
  upload: robots.txt 
  • 网址:/.* 脚本:main.application

正确的 MAIN.PY

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)

【问题讨论】:

    标签: python google-app-engine python-2.7


    【解决方案1】:

    Appengine 不支持 3x(还没有?)。您遇到的错误是什么?如果它是错误的,应该在日志中打印一些东西。一对夫妇认为我注意到了,

    • 我认为您不再需要 run_wsgi_app
    • yaml 中的脚本应该是“main.application”而不是“main.py”。将应用程序变量作为全局变量而不是局部变量

    【讨论】:

    • main.application 并将application 设置为全局(而不是main 中的本地)。
    • 错误:服务器错误 服务器遇到错误,无法完成您的请求。如果问题仍然存在,请报告您的问题并提及此错误消息和导致它的查询
    • @DaveW.Smith 抱歉,但是……我该怎么做?猜这是在 app.yaml 中的吗?
    • app.yaml 中,更改为script: 行以使用main.application,在main.py 中,将application = ... 行移出main() 并使其成为类变量。或者,将其更改为app = ...(并将其移出main)。关键是让script: 指向一个类变量。
    • @DaveW.Smith 所以我更改了 main.application,我尝试移动 application = ...,但我不确定将它放在哪里。现在是代码。请进行任何必要的编辑,因为它不起作用。我会把它放在我的问题的底部。
    【解决方案2】:

    根据谷歌官方Migrating to Python 2.7 doc,我认为你至少要做两件事

    • 更新您的app.yaml

    在 GAE Python 2.5 中,url handler 的 script 属性是路径 到python源文件。

    - url: /.*
      script: main.py
    

    在 Python 2.7 中,改为 a 对象

    - url: /.*
      script: myapp.app
    

    app 是 webapp2.WSGIApplication 的一个实例

    • 将您的应用程序更新到 webapp2
    import webapp2
    
    class MainPage(webapp2.RequestHandler):
      def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, WebApp World!')
    
    app = webapp2.WSGIApplication([('/', MainPage)])
    
    """ Old code:
    def main():
      run_wsgi_app(app)
    
    if __name__ == '__main__':
      main()
    """
    

    尽管您希望保持模板不变,但不推荐使用 webapp 模板。

    webapp 模板现已弃用。在他们的地方,你可以使用 Jinja2、Django 或您选择的模板系统(只要是 用纯 Python 编写)。

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 2012-03-10
      • 2012-11-17
      • 2011-07-09
      • 1970-01-01
      • 2018-04-29
      • 2019-04-16
      • 2012-09-26
      • 2012-04-27
      相关资源
      最近更新 更多