0

I have a small script which reads data inside a file in chunks, encodes them and then runs curl to send a POST request for every chunk.

I would like to add a progress bar of the global upload status everytime the POST returns a '200 OK' instead of the upload status of every chunk

....
CHUNK_SIZE = 500
with open(file, "rb") as f:

    while True:
    
        chunk = (base64.b64encode(f.read(CHUNK_SIZE))).decode('ascii')
        if not chunk:
            print ("\n\n\n####COMPLETED####\n\n\n")
            break

        time.sleep(5)
        
        ...
        upload part
        ...

        if len(out) > 0:
            if (str(out, 'utf-8').find("200 OK") != -1):
                print ("\n\n\n####OK####\n\n\n")

            else:
            ....

So, for example, if a file has a size of 2000 bytes and the chunks are set to 500 bytes, for every chunk sent, this is the output I would like to have ( right now I just get an "ok" ) but I could not get:

chunk 1:

"25% done"

chunk 2:

"50% done"

chunk 3:

"75% done"

chunk 4:

"100% - completed"

EDIT:

        total = os.path.getsize(file)
        num_chunks_uploaded = 0
        max_chunks_tosend = total // CHUNK_SIZE
        ....
                if len(out) > 0:
                    if (str(out, 'utf-8').find("200 OK") != -1):
                        print ("\n\n\n####OK####\n\n\n")
                        if ( num_chunks_uploaded <= max_chunks_tosend ):
                            num_chunks_uploaded += 1
                            #print(num_chunks_uploaded)
                            print(int(num_chunks_uploaded * CHUNK_SIZE / total * 100))
ryout
  • 15
  • 6
  • [Text progress bar in terminal with block characters](https://stackoverflow.com/q/3173320) – 001 Mar 25 '22 at 14:06
  • @JohnnyMopp thanks, I know that link! But I could not the global update status. I mean, I tried with different things in there, I can get the status of every chunk uploded, yes, but not the global one – ryout Mar 25 '22 at 14:10
  • Can you add a variable to keep track of the number of chunks uploaded? Then the percent done is: `int(num_chunks_uploaded * CHUNK_SIZE / file_size_in_bytes * 100)`. – 001 Mar 25 '22 at 14:32
  • @JohnnyMopp Thanks, it works! The only problem is that it goes over 100% ahah! I have updated the question, so it is easier to understand. WIth the file I used as test, it reached the 112% – ryout Mar 25 '22 at 15:10
  • I coded some workaround to not get over 100%, its fine now. Thanks! Post an answer with your comment if you want to! – ryout Mar 25 '22 at 15:40

0 Answers0