Reputation: 1537
I have an ftp program using ftplib and for the past couple of days I have been trying to figure out how to implement a progress bar for retrbinary and storbinary. I am trying to use the progressbar 2.3 module but have had no luck in getting it to tie in. I think this may stem from a misunderstanding of how the callback function works. Anyway here is what I have.
import progressbar
import ftplib
ftp = ftplib.FTP()
ftp.connect("host", "port")
ftp.login("user", "pwd")
widgets = ['Downloading: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=1024).start()
def callback(p):
for i in range(1024):
pbar.update(10*i+1)
pbar.finish()
ftp.storbinary("STOR iTunesSetup.exe", open("iTunesSetup.exe"), callback, blocksize=1024)
I get this error although this is just one of many ways I have tried this:
Traceback (most recent call last):
File "progrbar.py", line 7, in <module>
ftp.connect("host", "port")
File "/usr/lib/python2.7/ftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -8] Servname not supported for ai_socktype
I can say without this callback the ftp server works just fine and has more then enough space to download. If someone can give me some pointers I would appreciate it.
Upvotes: 3
Views: 3100
Reputation: 1537
For any future visitors this is how I did it...
def handleupload(self, block):
self.pbar.update(self.pbar.currval+len(block))
def upload(self):
try:
if os.path.getsize(self.filename) == 0:
print "%s 0 bytes passing..." % self.filename
else:
print "Uploading %s-%d-bytes" % (self.filename, self.ftp.size(self.filename))
self.pbar=ProgressBar(widgets=[FileTransferSpeed(),' ', Bar(marker=RotatingMarker()), ' ',
Percentage(),' ', ETA()], maxval=os.path.getsize(self.filename)).start()
self.ftp.storbinary("STOR " + self.filename, open(self.filename),
callback = s.handleupload, blocksize = 1024)
self.pbar.finish()
print "Finished"
except (error_perm, error_temp, Exception), self.exc:
print self.exc
Upvotes: 6