python - Tornado x-www-form-urlencoded 主体无效 : 'latin-1' codec can't encode characters in position 774-777: ordinal not in range(256)

标签 python python-3.x character-encoding tornado

我正在使用 Tornado 接受从我无权访问的客户端发送的一些数据。如果数据中仅出现英文字符,则一切正常。当数据中包含 utf-8 编码的中文字符(3 个字节)时,Tornado 会给出此警告,并且“get_argument”函数根本无法获取任何内容。

我调试并简化了我的代码到最简单的,但警告仍然出现

class DataHandler(tornado.web.RequestHandler):
    def post(self):
        print("test")
        print(self.get_argument("data"))
        print("1")

application = tornado.web.Application([
    (r"/data", Data),
])

application.listen(5000)
tornado.ioloop.IOLoop.instance().start()

数据的格式如下所示:

data={"id":"00f1c423","mac":"11:22:33:44:55:66"}

数据采用x-www-form-urlencoded,WireShark显示中文字符是完美的3字节utf-8,以E(1110)开头。警告(774-777)中提到的位置是汉字的开始位置,尽管汉字发生了变化,但它始终是5个字节。

我对警告中的“编码”感到困惑。实际上,我在代码中没有对编码进行任何操作,因此我认为这就是 Tornado 在 RequestHandler 类中所做的事情。但是由于Tornado默认使用utf-8编解码器,那么这个latin-1从哪里来呢?最重要的是,我该如何解决它?

最佳答案

这将不再是问题。 Tornado 做了一些更改来支持 x-www-form-urlencoded 正文,其值由编码字节组成,但未 url 编码为 ascii。

参见:tornado merge request

另外:github issue #2733

关于python - Tornado x-www-form-urlencoded 主体无效 : 'latin-1' codec can't encode characters in position 774-777: ordinal not in range(256),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57657329/

相关文章:

python - gaesessions 问题 (django+GAE)

python - 如何比较python中的两个列表,对象实例

python - Windows 10 上的多处理 python 3.6 无法正常工作

c# - HttpClient 发布到 ApacheServer (Debian)

java - Spring MVC 响应编码问题

python - grpcio-tools protoc.main 给出 'missing output directives' 错误

python - 使用 Pandas 将数据分组为单独的批处理

python-3.x - 通过 openpyxl 使用公式从 Excel 单元格获取值

python - 初始化时出现 tkinter StringVar() 错误

python - 使用 django 正确编码 JSON Web 服务