2011-04-15 61 views
2

我上传的FTPLib在Python中的文件,并有一个cli加载栏与progressbar 2.2. 我需要一个加载栏来告诉上传的进度。Python的FTP上传栏

有没有人有任何有关该主题的信息?

感谢,giodamelio


由于塞特希库马兰指出的那样,在ftplib.storbinary功能的回调参数,但我不知道如何使用它。

我试过了。每次上传一个字节,我都希望它能打印这条消息。

import ftplib 

def callback(): 
    print("This is the callback function") 

s = ftplib.FTP('myserver.com','login','password') # Connect 

f = open('test.txt','rb')    # file to send 
s.storbinary('STOR test.txt', f, 1024, callback())   # Send the file 

f.close()        # Close file and FTP 
s.quit() 
+1

不要调用你的回调函数 - 你实际上是在传入None作为回调函数。 – JimB 2011-04-15 18:42:45

+0

我从来没有使用回调函数,所以我有点困惑。我应该改变哪一部分? – giodamelio 2011-04-15 18:46:26

回答

4

小改变了代码:

import ftplib 

def callback(p): 
    print("This is the callback function") 

s = ftplib.FTP('myserver.com','login','password') # Connect 

f = open('test.txt','rb')    # file to send 
s.storbinary('STOR test.txt', f, 1024, callback)   # Send the file 

f.close()        # Close file and FTP 
s.quit() 

回调需要在后面叫。如果在您将其作为参数传递时调用它,则会传递返回值。由于您的callback函数没有返回,它会通过None

+0

非常感谢。 – giodamelio 2011-04-15 18:52:25

+0

在附注上,'ftplib.storbinary'函数将每个数据块传递回回调函数,所以它必须是'def callback(param1):' – giodamelio 2011-04-15 18:54:04

2

像显示您尝试的代码示例的具体问题将有助于回答。 当FTP库提供callback函数的某些功能并使用进度指示器功能(在本例中为进度条)并将其附加到该回调函数中时,可以使用指示器显示进度。查看ftplib文档,有规定将回调附加到某些方法,这可能对您有用。

+0

darn ...一定很累。我在那里寻找一个回调函数,不知何故错过了它。将在早上提供更新的代码。 – giodamelio 2011-04-15 06:52:35