Have a look at the answer from @Spencer - it is the correct one. Here are some minor modifications for 2021 PEPs conventions and a modulo, to only update output every x
MB (or KB, or GB).
import logging, enum
from paramiko import SSHClient
class SIZE_UNIT(enum.Enum):
BYTES = 1
KB = 2
MB = 3
GB = 4
def convert_unit(size_in_bytes: int, unit: SIZE_UNIT):
"""Convert the size from bytes to
other units like KB, MB or GB
"""
if unit == SIZE_UNIT.KB:
return size_in_bytes/1024
elif unit == SIZE_UNIT.MB:
return size_in_bytes/(1024*1024)
elif unit == SIZE_UNIT.GB:
return size_in_bytes/(1024*1024*1024)
else:
return size_in_bytes
def progress(transferred: int, tobe_transferred: int):
"""Return progress every 50 MB"""
if convert_unit(transferred, SIZE_UNIT.MB) % 50 != 0:
return
logging.getLogger().info(
f"Transferred: "
f"{convert_unit(transferred, SIZE_UNIT.GB):.2f}GB \t"
f"out of: {convert_unit(tobe_transferred, SIZE_UNIT.GB):.2f}"
f"GB")
client = SSHClient()
client.connect(...)
sftp = client.open_sftp()
sftp.get(remotepath, localpath, callback=progress)