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"
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?
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.
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 :
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
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?). What is the error you encoutered? If it is error, should print something to the logs. A couple think that I notice,
According to Google's official Migrating to Python 2.7 doc, I think you have to do 2 things at least
app.yaml
. in GAE Python 2.5, the script attribute of the url handler is the path to the python source file.
- url: /.*
script: main.py
in Python 2.7 , it is changed to the a object
- url: /.*
script: myapp.app
app is a instance of the webapp2.WSGIApplication
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 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).
链接地址: http://www.djcxy.com/p/91624.html