开发者

Python FTP uploading bar

I am uploading a file with FTPLib in python and have a cli loadingbar with progressbar 2.2. I need t开发者_运维问答o make a loading bar to tell the progress of the upload.

Does anyone have any info on the topic?

Thanks, giodamelio


As Senthil Kumaran pointed out, there is a callback parameter in the ftplib.storbinary function but I do not know how to use it.

I tried this. I expected it to print the message every time a byte was uploaded.

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()


Small change to your code:

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()

The callback needs to be called later on. If you invoke it as you pass it as a parameter, it's return value is passed instead. Since your callback function has no return, it would pass in None.


A specific question like showing a code sample of what you tried would helpful to answer. The showing of progress using an indicator is possible when the FTP library provides certain facilities of callback functions and you use your progress indicator function (which in this case is progressbar) and attach it to that callback. Looking at the ftplib documentation, there are provisions to attach callbacks to certain methods, perhaps that should be useful for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜