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

結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM