1

I write a program to download file from a FTP server, here is the code:

from ftplib import FTP

host = 'x.x.x.x'
user = 'demo'
password = 'fakepw`'
path = '/mnt10/DATA/201605/'
filename = 'DATA.TXT'
ftp = FTP(host, user, password)   # connect to host, default port
ftp.cwd(path)
ftp.voidcmd('TYPE I')
sock = ftp.transfercmd('RETR ' + filename)
f = open('D:\\DATA\\' + filename, 'wb')
chunk = 1
while True:
    block = sock.recv(1024*1024)
    if not block:
        break
    print('Getting file {} bytes'.format(chunk*(1024**2)))
    chunk += 1
    f.write(block)
sock.close()

Here is the result of running program:

Getting file 1048576 bytes
Getting file 2097152 bytes
Getting file 3145728 bytes
Getting file 4194304 bytes
Getting file 5242880 bytes
..........................
Getting file 22020096 bytes
Getting file 23068672 bytes
Getting file 24117248 bytes
Getting file 25165824 bytes
Getting file 26214400 bytes

Process finished with exit code 0

But the real file size only around 11.5MB Real file size

I don't know why they are different.

Edit: As @Martin Prikryl answer, I have alter my program like this:

total_size = 0

while True:
    block = sock.recv(1024*1024)
    if not block:
        break
    print('Size of block: {}'.format(len(block)))
    total_size += len(block)
    f.write(block)

print('Total size: {}'.format(total_size))

sock.close()

And now the program is running well.

Size of block: 1048576
Size of block: 6924
Size of block: 1048576
......................
Size of block: 14924
Size of block: 771276
Total size: 11523750

Process finished with exit code 0
1
  • 2
    If you do a recv() of 1024x1024 bytes, you do not necessarily receive the full amount that you asked for. Commented May 19, 2016 at 7:44

1 Answer 1

2

If the socket is asynchronous, you do not get whole 1024*1024 bytes. You get only as many bytes as is available (was received already).

Print the size of the block instead, to get an accurate number of bytes read.

1
  • 1
    I have edited my program and now it operate as I wish. Thank you. Commented May 19, 2016 at 8:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.