treq
treq copied to clipboard
QUESTION: progress bar with download
Hi, I am trying to make a progress bar with treq download this is the code that I have written so far but there's a problem in line 14 because I can't find anything equivalent of response.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1)(this is from requests) in treq.
Is there any other way of making this possible or is there any other way to make progress bars with treq download?
I have figured out how to do the chunks part but now I am stuck on finding the content size of the received content.
If the server set a Content-Length header you can look there. If it didn't then that information isn't there at the HTTP level.
Perhaps we should be exporting the parsed content-length header as an attribute somewhere?
I am struggling with the same thing. I can't figure out how to get IResponse.length AND use treq.collect at the same time.
- Is there an example somewhere?
- If it's not possible, can
lengthbe exposed somewhere?
I figured it out, maybe it's useful to someone:
currentbytes = 0
totalbytes = None
def headers(response):
if response.length != twisted.web.iweb.UNKNOWN_LENGTH:
totalbytes = response.length
return response
def progress(packet):
# f.write(packet)
if totalbytes is not None:
currentbytes += len(packet)
percent = 100 * currentbytes / totalbytes
treq.get(url, unbuffered=True).addCallback(headers).addCallback(treq.collect, progress)