C Ftp Download File Progress Bar
Posted on by admin
I am trying to download a file from an FTP server with a progress bar. The file is downloading, and the ProgressChanged event is calling, except in the event args TotalBytesToReceive is always -1. TotalBytes increases, but I am unable to calculate the percentage without the total. Javad, I've made some preliminary progress, but nothing to show just yet. Currently I don't even have a user interface, but the results (including downloading a 2+ gig file with the ability to resume) looks promising.
Download file from FTP Server, upload file to S3 with Progress Bar in Python
Webclient Download File Progress
download_file_from_ftp_progress_bar.py
import progressbar |
from hurry.filesize import size |
filesize = ftp.size(file) |
try: |
down_progress = progressbar.AnimatedProgressBar(end=filesize, width=50) |
withopen(TEMP_DIR+file, 'wb') as f: |
defdownload_progress(chunk): |
f.write(chunk) |
down_progress +len(chunk) |
down_progress.show_progress() |
print('downloading: {} size: {}'.format(file, size(filesize))) |
ftp.retrbinary('RETR {}'.format(file), download_progress) |
print('n{} successfully downloaded'.format(file)) |
exceptExceptionas exc: |
print('Error downloading file {}!'.format(file), exc) |
progressbar.py
import sys |
import time |
classProgressBar(object): |
''ProgressBar class holds the options of the progress bar. |
The options are: |
start State from which start the progress. For example, if start is |
5 and the end is 10, the progress of this state is 50% |
end State in which the progress has terminated. |
width -- |
fill String to use for 'filled' used to represent the progress |
blank String to use for 'filled' used to represent remaining space. |
format Format |
incremental |
'' |
def__init__(self, start=0, end=10, width=12, fill='=', blank='.', format='[%(fill)s>%(blank)s] %(progress)s%%', incremental=True): |
super(ProgressBar, self).__init__() |
self.start = start |
self.end = end |
self.width = width |
self.fill = fill |
self.blank = blank |
self.format =format |
self.incremental = incremental |
self.step =100/float(width) #fix |
self.reset() |
def__add__(self, increment): |
increment =self._get_progress(increment) |
if100>self.progress + increment: |
self.progress += increment |
else: |
self.progress =100 |
returnself |
def__str__(self): |
progressed =int(self.progress /self.step) #fix |
fill = progressed *self.fill |
blank = (self.width - progressed) *self.blank |
returnself.format % {'fill': fill, 'blank': blank, 'progress': int(self.progress)} |
__repr__=__str__ |
def_get_progress(self, increment): |
returnfloat(increment *100) /self.end |
defreset(self): |
''Resets the current progress to the start point'' |
self.progress =self._get_progress(self.start) |
returnself |
classAnimatedProgressBar(ProgressBar): |
''Extends ProgressBar to allow you to use it straighforward on a script. |
Accepts an extra keyword argument named `stdout` (by default use sys.stdout) |
and may be any file-object to which send the progress status. |
'' |
def__init__(self, *args, **kwargs): |
super(AnimatedProgressBar, self).__init__(*args, **kwargs) |
self.stdout = kwargs.get('stdout', sys.stdout) |
defshow_progress(self): |
ifhasattr(self.stdout, 'isatty') andself.stdout.isatty(): |
self.stdout.write('r') |
else: |
self.stdout.write('n') |
self.stdout.write(str(self)) |
self.stdout.flush() |
if__name__'__main__': |
p = AnimatedProgressBar(end=100, width=80) |
whileTrue: |
p +5 |
p.show_progress() |
time.sleep(0.1) |
if p.progress 100: |
break |
print#new line |
upload_file_to_s3_with_progressbar.py
Ftp Command To Download File
import progressbar |
import boto3 |
from hurry.filesize import size |
bucket =BUCKET_NAME |
s3_client = boto3.resource('s3') |
s3_dir ='some_type_of_file/' |
filesize = ftp.size(file) |
try: |
# upload file to s3 |
print('nuploading {} size: {}'.format(file, size(filesize))) |
up_progress = progressbar.AnimatedProgressBar(end=filesize, width=50) |
defupload_progress(chunk): |
up_progress + chunk # Notice! No len() |
up_progress.show_progress() |
s3_client.meta.client.upload_file(file, bucket, s3_path, Callback=upload_progress) |
# s3_client.meta.client.upload_file(file, bucket, s3_path) # dont show progress |
print('nFile {} uploaded to S3 bucket:{} path:{}'.format(file, bucket, s3_dir)) |
except: |
print('Error uploading file {} to s3!'.format(file)) |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment