计算Paramiko中文件传输的剩余时间
Calculating remaining time of file transfer in Paramiko
这是我的代码:
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')
我得到以下输出:
Total size:30.6|Sent:30.5 MB|Remaining:0.1 MB
我的预期输出是:
Total size:30.6|Sent:30.5 MB|Remaining:0.1 MB|Time remaining:00:00:01
Paramiko中有没有可以给我时间戳的模块?如果没有,我该如何实现?
- 记住传输开始的时间;
- 在每次更新时,计算传输已经进行了多长时间;
- 据此计算传输速度;
- 据此,计算一下,剩下的要转多久。
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)))
另请注意,您的 MB 计算仅适用于 Python 3。在 Python 2 中,您将去除所有数字。我已经通过强制转换为 float
.
来解决这个问题
这是我的代码:
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')
我得到以下输出:
Total size:30.6|Sent:30.5 MB|Remaining:0.1 MB
我的预期输出是:
Total size:30.6|Sent:30.5 MB|Remaining:0.1 MB|Time remaining:00:00:01
Paramiko中有没有可以给我时间戳的模块?如果没有,我该如何实现?
- 记住传输开始的时间;
- 在每次更新时,计算传输已经进行了多长时间;
- 据此计算传输速度;
- 据此,计算一下,剩下的要转多久。
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)))
另请注意,您的 MB 计算仅适用于 Python 3。在 Python 2 中,您将去除所有数字。我已经通过强制转换为 float
.