将Google App Engine更新为Python 2.7

时间:2021-11-15 01:19:42

I've tried to update my app before by changing

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

runtime: python27 , threadsafe: true , script: main.app"

runtime:python27,threadsafe:true,script: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. It is running on 2.5 now (because I want to keep it up). robots.txt is empty. How can I update it to 2.7 or should I update it to 3.x?

它确实有效,并且在python 2.7上但它运行不正常我猜是因为当我访问网址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 ()

I'd prefer not to upload my index.html, but I'm sure it's working properly.

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

Edit:

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
  • url:/。*脚本: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)

2 个解决方案

#1


0  

Appengine does not support 3x (yet?). What is the error you encoutered? If it is error, should print something to the logs. A couple think that I notice,

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

  • 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". Put the application variable as a global variable instead of local variable
  • yaml中的脚本应该是“main.application”而不是“main.py”。将应用程序变量作为全局变量而不是局部变量

#2


0  

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处理程序的脚本属性是python源文件的路径。

- url: /.*
  script: main.py

in Python 2.7 , it is changed to the a object

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

- 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. In their place, you can use Jinja2, Django, or a templating system of your choice (as long as it's written in pure Python).

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

#1


0  

Appengine does not support 3x (yet?). What is the error you encoutered? If it is error, should print something to the logs. A couple think that I notice,

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

  • 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". Put the application variable as a global variable instead of local variable
  • yaml中的脚本应该是“main.application”而不是“main.py”。将应用程序变量作为全局变量而不是局部变量

#2


0  

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处理程序的脚本属性是python源文件的路径。

- url: /.*
  script: main.py

in Python 2.7 , it is changed to the a object

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

- 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. In their place, you can use Jinja2, Django, or a templating system of your choice (as long as it's written in pure Python).

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