使用httplib2限制响应大小 [英] Limiting response size with httplib2
问题描述
是否可以使用httplib2限制响应大小?例如,如果它看到超过X字节的HTTP主体,则连接将关闭而不消耗更多带宽。或者也许只下载文件的前X个字节。
Is it possible to limit the response size with httplib2? For instance if it sees an HTTP body over X bytes the connection will just close without consuming more bandwidth. Or perhaps only download the first X bytes of a file.
推荐答案
假设服务器正在发送响应体大小 Content-Length
响应标题字段,你可以自己动手。
Assuming that the server is sending the response body size in the Content-Length
response header field, you can do it yourself.
首先,调用 Http .request(method =HEAD)
只检索标题而不是正文。然后检查响应的 Content-Length
字段,看它是否低于您的阈值。如果是,请使用正确的 GET
或 POST
方法发出第二个请求以检索正文;否则会产生错误。
First, call Http.request(method="HEAD")
to retrieve only the headers and not the body. Then inspect the Content-Length
field of the response to see if it is below your threshold. If it is, make a second request with the proper GET
or POST
method to retrieve the body; otherwise produce an error.
如果服务器没有给你Content-Length(或说谎的话),看起来就像是有办法在一些字节后切断下载。
If the server isn't giving you the Content-Length (or is lying about it), it doesn't look like there is a way to cut off the download after some number of bytes.
这篇关于使用httplib2限制响应大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!