CherryPy 如何读取分块编码请求 Body

CherryPy How to Read Chunked Encoded Request Body

我尝试从 POST body 中获取具有 Transfer-Encoding: chunked header(没有 Content-Length)的数据。 content-type 是 application/octet-stream.

我试过了

cherrypy.request.body.read()

但它冻结并返回 500

我也试过了

buffer = cherrypy.request.body.read(1024)

但不知道什么时候停止阅读。 有人有建议吗?

这是我最终使用的代码。

def POST(self):
    f = open('tmp','wb')
    cherrypy.request.rfile.bufsize = 1024 * 1024 * 5  #adjust buffer size here
    while True:
        cherrypy.request.rfile._fetch();              #reading data
        if cherrypy.request.rfile.closed:             #end of stream checking
            break
        buffer = cherrypy.request.rfile.buffer        #your data is here
        cherrypy.request.rfile.buffer = ""            #clearing buffer
        f.write(buffer)                               #consume it

    f.close()
    return "done"