4

我正在尝试在 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_callback 选项。但是当后端服务器禁用分块响应时,不会调用回调。

如何处理大内容?

谢谢。

4

1 回答 1

1

您应该能够传递max_buffer_sizetornado.httpclient.AsyncHTTPClient() 调用以设置最大缓冲区大小。对于 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()

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

于 2013-09-04T13:56:55.217 回答