搜索
您的当前位置:首页正文

5、响应输出

来源:二三娱乐

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
     and
    
    """
    pass

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

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

缓冲区刷新的几种方式:

  • 程序结束
  • 手动刷新
  • 缓冲区满了
  • 遇到\n

示例:

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

Top