使用 Webapp2 部署的 GAE RequestHandler 时出错

发布于 2024-12-22 03:16:14 字数 1375 浏览 2 评论 0原文

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

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

代码如下:

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 (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

此错误意味着什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-12-29 03:16:14

我认为响应不采用 unicode 数据,因此您必须编码它第一:

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

我还推荐Werkzeug。它在 appengine 上运行良好,让生活变得更加轻松。它有助于处理请求和响应数据、url 路由、提供 http 异常、具有用于离线开发的强大调试器等等。我认为 Werkzeug 是每个 Python Web 开发者工具箱中必备的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文