这可能是个愚蠢的问题,但我找不到答案。此外,它可能不是真的扭曲具体。在
我试图为twisted.web
web服务器编写一个资源,该服务器应该为包含非ascii字符的页面提供服务。
根据this discusson,我需要做的就是设置Content-Type
HTTP头并返回一个编码字符串。
不幸的是,页面显示无效字符。在
以下是代码(作为.rpy
):
"""a unicode test"""
from twisted.web.resource import Resource
class UnicodeTestResource(Resource):
"""A unicode test resource."""
isLeaf = True
encoding = "utf-8"
def render_GET(self, request):
text = u"unicode test\n ä ö ü ß"
raw = u"<HTML><HEAD><TITLE>Unicode encoding test</TITLE><HEAD><BODY><P>{t}</P></BODY></HTML>".format(t=text)
enc = raw.encode(self.encoding)
request.setHeader("Content-Type", "text/html; charset=" + self.encoding)
return enc
resource = UnicodeTestResource()
结果(没有html)是:unicode test ä ö ü Ã
。在
这是由服务器和客户端之间的编码不匹配引起的吗?在
我使用的是python2.7.12和twisted 17.1.0。已经使用firefox访问了该页面。在
对不起我糟糕的英语。在
谢谢
编辑:我发现了问题。我假设带有ResourceScript
处理器的twisted.web.static.File
将使用运行reactor的文件中指定的编码。
显然情况并非如此。
在每个文件的顶部添加# -*- coding: utf-8 -*-
修复了这个问题。在
目前没有回答
相关问题 更多 >
编程相关推荐