Python3 WSGI在浏览器中显示字节串

时间:2015-01-13 12:00:05

标签: python python-3.x mod-wsgi wsgi

我正在使用mod_wsgi在Python3中构建一个Web应用程序。一切顺利,没有错误,除了我一直得到:

B'

在我的回复中传递给浏览器。此外,任何使用" \"角色也会在回复中显示出来。

以下是返回内容的函数示例:

def index(self):
    # self.default_encode = 'UTF-8'
    self.header("Content-type", "text/html;")
    return_string = Index.index()
    self.header("Content-Length", str(len(return_string)))
    return return_string.encode(self.default_encode)

以下是我如何将我的回复返回给浏览器:

def __iter__(self):
    x = self.delegate()
    self.start(self.status, self._headers)

    if isinstance(x, str):
        return iter(repr([x]))
    else:
        return iter(repr(x))

我将所有内容编码为UTF-8,并且不确定为什么我无法解决这个问题。我搜索了SO,Google和Bing,并尝试将其转换为bytes()。它可能很有用,但是当我从libapache2-mod-wsgi切换到libapache2-mod-wsgi-py3时,就会发生这种情况。

提前感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

我想出来了,我需要:

yield return_string.encode(self.default_encode)

而不是:

return return_string.encode(self.default_encode)