Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QUESTION: progress bar with download #236

Open
mzfr opened this issue Feb 9, 2019 · 5 comments
Open

QUESTION: progress bar with download #236

mzfr opened this issue Feb 9, 2019 · 5 comments
Labels

Comments

@mzfr
Copy link

mzfr commented Feb 9, 2019

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?

@mzfr
Copy link
Author

mzfr commented Feb 10, 2019

I have figured out how to do the chunks part but now I am stuck on finding the content size of the received content.

@twm
Copy link
Contributor

twm commented Jun 5, 2019

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.

@glyph
Copy link
Member

glyph commented Aug 3, 2019

Perhaps we should be exporting the parsed content-length header as an attribute somewhere?

@twm twm added the question label Mar 14, 2020
@scriptmelvin
Copy link

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 length be exposed somewhere?

@scriptmelvin
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants