我有一个使用ftplib的ftp程序,在过去的几天里,我一直在试图找出如何实现retrbinary和storbinary的进度条。我正在尝试使用进度条2.3模块,但却没有把它与它结合起来。我认为这可能源于对回调函数如何工作的误解。无论如何,这就是我所拥有的。
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)
我收到此错误,虽然这只是我尝试过的众多方法之一:
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
我可以说没有这个回调,ftp服务器工作正常,有足够的空间下载。如果有人可以给我一些指示,我会很感激。
答案 0 :(得分:6)
对于任何未来的访客,我就是这样做的...
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