Python 3.x 如何在Python3上使用tornado代理大型内容?

Python 3.x 如何在Python3上使用tornado代理大型内容?,python-3.x,tornado,reverse-proxy,Python 3.x,Tornado,Reverse Proxy,我试图在Python3上用tornado实现异步http反向代理 处理程序类如下所示: class RProxyHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): backend_url = 'http://backend-host/content.html' # temporary fixed req = tornado.httpcli

我试图在Python3上用tornado实现异步http反向代理

处理程序类如下所示:

class RProxyHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    def get(self):
        backend_url = 'http://backend-host/content.html'   # temporary fixed

        req = tornado.httpclient.HTTPRequest(
                                    url=backend_url)
        http_client = tornado.httpclient.AsyncHTTPClient()
        http_client.fetch(req, self.backend_callback)

    def backend_callback(self, response):
        self.write(response.body)
        self.finish()
当content.html较小时,此代码可以正常工作。但对于大型content.html,此代码会引发异常:

ERROR:tornado.general:Reached maximum read buffer size
我找到了用pycurl处理大型内容的方法。不过,它似乎不适用于Python3

此外,我还向HTTPRequest添加了streaming_回调选项。但当后端服务器禁用分块响应时,将不会调用回调

我如何处理大的内容


谢谢。

您应该能够将
max\u buffer\u size
传递给
tornado.httpclient.asynchtpclient()
调用以设置最大缓冲区大小。对于50MB缓冲区:

import tornado.ioloop
import tornado.web
from tornado.httpclient import AsyncHTTPClient
from tornado import gen
from tornado.web import asynchronous


class MainHandler(tornado.web.RequestHandler):
    client = AsyncHTTPClient(max_buffer_size=1024*1024*150)

    @gen.coroutine
    @asynchronous
    def get(self):
        response = yield self.client.fetch("http://test.gorillaservers.com/100mb.bin", request_timeout=180)
        self.finish("%s\n" % len(response.body))

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

更新:现在是完整的示例程序。

谢谢您的回答。不过,响应大小一如既往地受到限制。我可以转达以下内容吗?(从后端接收)->(存储到缓冲区)->(发送到客户端)->(清除缓冲区)->(从后端接收)…我已将示例更新为一个完整的程序,它将下拉一个100mb的文件。这几乎就是您描述的流,除非您指的是流代理。您必须编写一个完整的HTTP处理程序来实现流代理,因为您需要连接到IOStream。当内容大小大于最大缓冲区大小时,此程序引发“已达到最大读取缓冲区大小”异常。所以,我猜整个内容都存储到了缓冲区…@bobunderson:这方面有什么进展吗?如果您使用
fetch()
/
HTTPRequest()
streaming\u callback
参数进行逐块下载,我想使用tornado作为大型(如多GB)文件fyi的代理,您可以设置
max\u body\u size
而不是
max\u buffer\u size
来增加整个下载的大小限制,而不增加每个块的大小限制。有关更多信息,请参阅。此答案可能会有所帮助