3

我有一个使用 ftplib 的 ftp 程序,在过去的几天里,我一直在试图弄清楚如何为 retrbinary 和 storbinary 实现进度条。我正在尝试使用progressbar 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 服务器就可以正常工作并且有足够的空间来下载。如果有人可以给我一些指示,我将不胜感激。

4

1 回答 1

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
于 2012-07-26T19:17:05.097 回答