有没有库可以以文件方式读取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:
chunk = response.read(1024)
print "Chunk Length: {}".format(len(chunk))
有没有这样的API?我只想在调用read
方法时从源头读取数据,而不是在内存中提前加载响应的内容(除了头信息)直到我需要它。