Ravish Mallya
Ravish Mallya

Reputation: 47

Calculating remaining time of file transfer in Paramiko

Here is my code:

def send_file(self,source,destination):
        self.client.open_sftp().put(source,destination,self.upload_status)

def upload_status(self,sent,size):    
    sent_mb=round(sent/1000000,1)
    remaining_mb=round((size-sent)/1000000,1)
    size=round(size/1000000,1)
    sys.stdout.write("Total size:{0} MB|Sent:{1} MB|Remaining:{2} MB".
        format(size,sent_mb,remaining_mb))
    sys.stdout.write('\r')

I get the following output:

Total size:30.6|Sent:30.5 MB|Remaining:0.1 MB

My expected output is:

Total size:30.6|Sent:30.5 MB|Remaining:0.1 MB|Time remaining:00:00:01

Is there any module in Paramiko that can give me time stamp? If not, how can I achieve this?

Upvotes: 2

Views: 979

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202158

  • Remember time, when the transfer started;
  • On each update, calculate how long the transfer is already taking place;
  • Based on that, calculate a transfer speed;
  • Based on that, calculate, how long will it take to transfer the rest.
import datetime

# ...

start = datetime.datetime.now()

def upload_status(self, sent, size):
    sent_mb = round(float(sent) / 1000000, 1)
    remaining_mb = round(float(size - sent) / 1000000, 1)
    size_mb = round(size / 1000000, 1)
    time = datetime.datetime.now()
    elapsed = time - start
    if sent > 0:
        remaining_seconds = elapsed.total_seconds() * (float(size - sent) / sent)
    else:
        remaining_seconds = 0
    remaining_hours, remaining_remainder = divmod(remaining_seconds, 3600)
    remaining_minutes, remaining_seconds = divmod(remaining_remainder, 60)
    print(
        ("Total size:{0} MB|Sent:{1} MB|Remaining:{2} MB|" +
         "Time remaining:{3:02}:{4:02}:{5:02}").
        format(
            size_mb, sent_mb, remaining_mb,
            int(remaining_hours), int(remaining_minutes), int(remaining_seconds)))

Also note that your MB calculation works in Python 3 only. In Python 2, you would be striping all digits. I've fixed that by a cast to float.

Upvotes: 1

Related Questions