将Google App Engine更新为Python 2.7

我试图通过更改来更新我的应用程序

运行时:python27,threadsafe:true,脚本:main.app“

它确实工作,并在Python 2.7,但它没有正常运行我猜,因为我的index.html没有显示时,我去了http://dhsenviro.appspot.com网址。 它现在运行在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 
  • url:/.* script: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)
    

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

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

  • 根据谷歌官方迁移到Python 2.7 doc,我认为你至少必须做2件事

  • 更新你的app.yaml
  • 在GAE Python 2.5中,url处理程序的脚本属性是python源文件的路径。

    - url: /.*
      script: main.py
    

    在Python 2.7中,它被更改为一个对象

    - 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编写的)。

    链接地址: http://www.djcxy.com/p/91623.html

    上一篇: Update Google App Engine to Python 2.7

    下一篇: App Engine Python Modules and channel service