5、响应输出

2018-06-03  本文已影响10人  马小跳_

write

def write(self, chunk):
    """Writes the given chunk to the output buffer.

    To write the output to the network, use the flush() method below.

    If the given chunk is a dictionary, we write it as JSON and set
    the Content-Type of the response to be ``application/json``.
    (if you want to send JSON as a different ``Content-Type``, call
    set_header *after* calling write()).

    Note that lists are not converted to JSON because of a potential
    cross-site security vulnerability.  All JSON output should be
    wrapped in a dictionary.  More details at
    http://haacked.com/archive/2009/06/25/json-hijacking.aspx/ and
    https://github.com/facebook/tornado/issues/1009
    """
    pass

将chunk数据写到输出缓冲区。

缓冲区数据刷新到浏览器是由socket来控制。

缓冲区刷新的几种方式:

示例:

class DocHandler(RequestHandler):
    def get(self, document_id, *args, **kwargs):
        self.write(chunk=document_id)
        self.write("\r\n")
        self.write(chunk={'a': 1, 'b': 2})  # 利用write方法写json数据

finish

刷新缓冲区,关闭当次请求通道

不要在finish下边write

上一篇 下一篇

猜你喜欢

热点阅读