简体   繁体   English

将Google App Engine更新为Python 2.7

[英]Update Google App Engine to Python 2.7

I've tried to update my app before by changing 我尝试过通过更改来更新我的应用

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

It did work and was on python 2.7 but it didn't run properly I guess because my index.html didn't display when I went to the url http://dhsenviro.appspot.com . 它确实可以运行,并且在python 2.7上运行,但是我不能正常运行,因为当我转到URL http://dhsenviro.appspot.com时,index.html没有显示。 It is running on 2.5 now (because I want to keep it up). 它现在在2.5上运行(因为我想继续使用)。 robots.txt is empty. robots.txt为空。 How can I update it to 2.7 or should I update it to 3.x? 如何将其更新为2.7或应将其更新为3.x?

app.yaml : 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 : 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 ()

I'd prefer not to upload my index.html, but I'm sure it's working properly. 我不希望上传我的index.html,但是我确定它可以正常工作。

Edit: 编辑:

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, {}))

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

def main ():

if __name__ == '__main__':
  main ()

CORRECT APP.YAML : 正确的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.application

CORRECT 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 does not support 3x (yet?). Appengine不支持3x(还?)。 What is the error you encoutered? 您遇到了什么错误? If it is error, should print something to the logs. 如果错误,则应在日志中打印一些内容。 A couple think that I notice, 一对夫妇认为我注意到了,

  • I don't think you need the run_wsgi_app anymore 我认为您不再需要run_wsgi_app
  • the script in yaml should be "main.application" instead of "main.py". yaml中的脚本应为“ main.application”而不是“ main.py”。 Put the application variable as a global variable instead of local variable 将应用程序变量作为全局变量而不是局部变量

According to Google's official Migrating to Python 2.7 doc, I think you have to do 2 things at least 根据Google的官方迁移到Python 2.7文档,我认为您至少必须做两件事

  • Update your app.yaml . 更新您的app.yaml

in GAE Python 2.5, the script attribute of the url handler is the path to the python source file. 在GAE Python 2.5中,URL处理程序的script属性是python源文件的路径。

 - url: /.* script: main.py 

in Python 2.7 , it is changed to the a object 在Python 2.7中,将其更改为a对象

 - url: /.* script: myapp.app 

app is a instance of the webapp2.WSGIApplication app是webapp2.WSGIApplication的实例

  • Updating your application to webapp2 将您的应用程序更新到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() """ 

Though you want to keep templates unchanged, webapp templates are deprecated. 尽管您希望模板保持不变,但不建议使用Webapp模板。

webapp templates are now deprecated. webapp模板现已弃用。 In their place, you can use Jinja2, Django, or a templating system of your choice (as long as it's written in pure Python). 在它们的位置,您可以使用Jinja2,Django或您选择的模板系统(只要它是用纯Python编写的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM