[Tutor] FTP Upload

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jul 15 13:55:16 EDT 2020


Always reply-ALL orReply-List to include the tutor list.

On 15/07/2020 17:09, John Weller wrote:
>> 1) implement the callback and use it to display progress, just to check that the
>> number of blocks is what you'd expect for the filesize
>>
> I'm struggling to understand callback.  As I know the problem, ie bytes being dropped,> would callback be of any benefit?  If so, how would I implement it.

>> def callback(block):
>>     ....
>>     print(....)

At the simplest level just  store how many times callback gets called.
(It should be once every block) so you can see if the expected number of
blocks are being sent. In particular whether the final (partial) block
is being sent...

You could do fancier stuff like check the size of block each time and
print that:

count = 0

def callback(black):
    global count
    count += 1
    print("Block %d sent with size %d" % (count, len(block))


cmd = "STOR %s" % "my_graph.png"
fd = open("my_graph.png", "rb")
if fd: # I assume open() returns False if it can't open the file
 	ftp.storbinary(cmd, fd, callback)fd.close()
print ("A total of %d blocks sent" % count)

Assuming your files are not massive. If they are then simply
limit the prints to every 10th or if len(block) != blocksize

Note, none of that is tested, just based on reading
the docs on callback!... :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list