Python 用于以类似文件的方式读取HTTP响应的库?
我需要使用Python发出HTTP请求以下载一个大文件,但我需要能够使用类似文件的指针读回响应块,有点像下面的伪代码:Python 用于以类似文件的方式读取HTTP响应的库?,python,http,Python,Http,我需要使用Python发出HTTP请求以下载一个大文件,但我需要能够使用类似文件的指针读回响应块,有点像下面的伪代码: request = HTTPRequest(GET, "http://localhost/bigfile.bin") request.send() response = request.get_response() print "File is {} bytes long.".format(response.content_length) while True: c
request = HTTPRequest(GET, "http://localhost/bigfile.bin")
request.send()
response = request.get_response()
print "File is {} bytes long.".format(response.content_length)
while True:
chunk = response.read(1024)
print "Chunk Length: {}".format(len(chunk))
有这样的API吗?每当调用read
方法时,我只想从源代码中读取,在我需要之前,不会将响应中的任何内容(标题除外)带入内存。是。看看这本书
在访问响应正文之前,可以使用避免获取响应正文:
req = requests.get('http://localhost/bigfile.bin', stream=True)
print "File is {} bytes long.".format(req.headers['Content-Length'])
while True:
chunk = req.raw.read(1024)
print "Chunk Length: {}".format(len(chunk))
我得到了一个
TypeError:request()得到了一个意外的关键字参数'stream'
@TK:Hmm。在这里对我有用(Windows,Python 2.7 x64,来自PyPI的请求的最新版本)。出于某种原因,我遇到了一个奇怪的问题,easy\u install
只安装了版本0.8。现已修复。@TK:Great:-)配置问题很烦人!对。奇怪的是,pip
安装了正确的版本,而easy\u install
没有安装。