在使用Webapp2的已部署GAE RequestHandler中出错 [英] Error in deployed GAE RequestHandler using Webapp2

查看:101
本文介绍了在使用Webapp2的已部署GAE RequestHandler中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google App Engine上使用了webapp2框架,并且在我的一个请求处理程序中发现了一个基本错误。

该应用程序运行正常本地实例,但会在已部署的Google App Engine版本上导致以下跟踪:



以下是代码:

 导入os 
from google.appengine.ext.webapp导入模板
导入webapp2
导入日志记录

class MainHandler( webapp2.RequestHandler):
def get(self):
logging.info('hi there 34')
template_values = {}
self.response.out.write(' hello world 4')
path = os.path.join(os.path.dirname(__ file__),'index.html')

##这是导致bug的代码##
self.response.out.write(template.render(path,template_values))
## ## ## ##

debug = os.environ.get ('SERVER_SOFTWARE','').startswith('Dev')

app = webap p2.WSGIApplication(
[(r'/ main',MainHandler)],
debug = debug)

def main():
app.run

回溯错误:

  Traceback(最近一次调用最后一次):

文件/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py,
line 86 ,运行

self.finish_response()
文件/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py,
line 127,in finish_response

self.write(data)

文件/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py,
line 202,写入

中断言类型(data)是StringType,write()参数必须是字符串

AssertionError:write()参数必须是字符串

这个错误的含义是什么?

解决方案

我认为响应不会采用unicode数据,因此您必须

  content = template。 render(path,template_values)
self.response.out.write(content.encode('utf-8'))

另外,我建议 Werkzeug 。它在appengine上运行良好,使生活变得如此简单。它有助于处理请求和响应数据,URL路由,提供http异常,对离线开发和更多的调试器。我认为Werkzeug是他们的工具箱中每个python web开发人员必须拥有的。


I am using the webapp2 framework on Google App Engine, and I'm getting a basic error in one of my Request Handlers.

The app is running ok in the local instance, but causes the following traceback on the deployed version of Google App Engine:

Here's the code:

import os
from google.appengine.ext.webapp import template
import webapp2
import logging 

class MainHandler(webapp2.RequestHandler):
    def get(self):
        logging.info('hi there 34')
        template_values = {}
        self.response.out.write('hello world 4')
        path = os.path.join(os.path.dirname(__file__), 'index.html')

        ## This is the code that causes the bug ##
        self.response.out.write(template.render(path, template_values))
        ## ## ## ##

debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')

app = webapp2.WSGIApplication(
    [(r'/main', MainHandler)], 
    debug = debug)

def main():
    app.run()

traceback error:

Traceback (most recent call last):

File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", 
line 86, in run

self.finish_response()
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", 
line 127, in finish_response

self.write(data)

File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", 
line 202, in write

assert type(data) is StringType,"write() argument must be string"

AssertionError: write() argument must be string

What does this error mean?

解决方案

I think response does not take unicode data, so you have to encode it first:

content = template.render(path, template_values)
self.response.out.write(content.encode('utf-8'))

Also I recommend Werkzeug. It works well on appengine and makes life so much easier. It helps to deal with request and response data, url routing, provides http exceptions, has great debugger for offline development and more. I think Werkzeug is a must to have for every python web dev in their toolbox.

这篇关于在使用Webapp2的已部署GAE RequestHandler中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆