Python ftplib and storbinary

Trying to understand how it works ftplib.

I am trying to save a file on an FTP server and implement a callback.

The documentation says:

FTP.storbinary(command, file[, blocksize, callback, rest])
Function

callback defined as in the documentation:

A callback function is called for each block of received data, with a single-line argument giving the data block.

How to implement this callback? The callback in retrbinary(reading the file) might look like this:

def handle(block):
    f.write(block)
    print ".", 

The file download progress will be displayed, fis a file object.

But I do not understand how to implement this with storbinary.

Any suggestions on how to do this? I know about a parameter block, but how do I configure it at boot time?

UPDATE:

I have a callback to load:

def handle(block):
    f.read(block)
    print ".",

, , :

an integer is required

int(block) .

+3
1

def handle(block):
    f.write(block)
    print ".", 

Python , params - - -

ftp.storbinary(command="stor someFileNameOnServer", file=open("localFile",'rb'), callback=handle,blocksize=1024)

python

callback - , , .

, . , . 1024 .

, - -

sizeWritten = 0
totalSize = someMethodToGetTotalBytesInFile()
def handle(block):
    global sizeWritten
    sizeWritten += 1024
    percentComplete = sizeWritten / totalSize
    print "%s percent complete" %str(sizeWritten / totalSize)

os.path.getsize .

+5
source

Source: https://habr.com/ru/post/1792621/


All Articles