tornado的self.write输入字典数据与json的dumps方法的ensure_ascii方法的说明

测试的tornado项目如下:

# -*- coding:utf-8 -*-
import json
import tornado.httpserver
import tornado.ioloop
from tornado.web import RequestHandler,Application
import tornado.options
from tornado.options import define,options
from tornado.escape import json_decode,json_encode

define("port",default=9104,help="run on the gevent port",type=int)
define("host",default="127.0.0.1",help="host aaa",type=str)

class ReverseHandler(RequestHandler):
    def get(self,word):
        self.write(word[::-1])

class IndexHandler(RequestHandler):
    def post(self):
        # req = self.request.body
        # print("type(req):",type(req))
        # req_de = json_decode(req)
        # print("type(req_de):",type(req_de))
        ret = {"name":"王宏伟"}
        
        ret = json.dumps(ret) # 如果直接这样序列化的话,字典中的中文会被编码成ASCII码
        self.write(ret)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = Application(
        handlers = [
            (r"/reverse/(\w+)",ReverseHandler),
            (r"/index",IndexHandler),
        ],
        debug = True,
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

结果如下:

在json.dumps方法中加上ensure_ascii=False,就可以输出中文了:

# -*- coding:utf-8 -*-
import json
import tornado.httpserver
import tornado.ioloop
from tornado.web import RequestHandler,Application
import tornado.options
from tornado.options import define,options
from tornado.escape import json_decode,json_encode

define("port",default=9104,help="run on the gevent port",type=int)
define("host",default="127.0.0.1",help="host aaa",type=str)

class ReverseHandler(RequestHandler):
    def get(self,word):
        self.write(word[::-1])

class IndexHandler(RequestHandler):
    def post(self):
        # req = self.request.body
        # print("type(req):",type(req))
        # req_de = json_decode(req)
        # print("type(req_de):",type(req_de))
        ret = {"name":"王宏伟"}
        
        ret = json.dumps(ret,ensure_ascii=False)
        self.write(ret)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = Application(
        handlers = [
            (r"/reverse/(\w+)",ReverseHandler),
            (r"/index",IndexHandler),
        ],
        debug = True,
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

结果如下:

当然self.write可以直接返回字典数据:

# -*- coding:utf-8 -*-
import json
import tornado.httpserver
import tornado.ioloop
from tornado.web import RequestHandler,Application
import tornado.options
from tornado.options import define,options
from tornado.escape import json_decode,json_encode

define("port",default=9104,help="run on the gevent port",type=int)
define("host",default="127.0.0.1",help="host aaa",type=str)

class ReverseHandler(RequestHandler):
    def get(self,word):
        self.write(word[::-1])

class IndexHandler(RequestHandler):
    def post(self):
        # req = self.request.body
        # print("type(req):",type(req))
        # req_de = json_decode(req)
        # print("type(req_de):",type(req_de))
        ret = {"name":"王宏伟"}
        self.write(ret)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = Application(
        handlers = [
            (r"/reverse/(\w+)",ReverseHandler),
            (r"/index",IndexHandler),
        ],
        debug = True,
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

结果如下:

posted on 2020-03-25 16:36  江湖乄夜雨  阅读(1417)  评论(0编辑  收藏  举报