Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTP library error: got more than 8192 bytes

Python fails while uploading a file which its size bigger than 8192 bytes. And the exception is only "got more than 8192 bytes". Is there a solution to upload larger files.

try:
    ftp = ftplib.FTP(str_ftp_server )
    ftp.login(str_ftp_user, str_ftp_pass)
except Exception as e:
    print('Connecting ftp server failed')
    return False

try:
    print('Uploading file ' + str_param_filename)
    file_for_ftp_upload = open(str_param_filename, 'r')
    ftp.storlines('STOR ' + str_param_filename, file_for_ftp_upload)

    ftp.close()
    file_for_ftp_upload.close()
    print('File upload is successful.')
except Exception as e:
    print('File upload failed !!!exception is here!!!')
    print(e.args)
    return False

return True
like image 288
bahtiyartan Avatar asked Jan 19 '15 00:01

bahtiyartan


2 Answers

storlines reads a text file one line at a time, and 8192 is the maximum size of each line. You're probably better off using, as the heart of your upload function:

with open(str_param_filename, 'rb') as ftpup:
    ftp.storbinary('STOR ' + str_param_filename, ftpup)
    ftp.close()

This reads and stores in binary, one block at a time (same default of 8192), but should work fine for files of any size.

like image 84
Alex Martelli Avatar answered Nov 10 '22 08:11

Alex Martelli


I had a similar issue and solved it by increasing the value of ftplib's maxline variable. You can set it to any integer value you wish. It represents the maximum number of characters per line in your file. This affects uploading and downloading.

I would recommend using ftp.storbinary in most cases per Alex Martelli's answer, but that was not an option in my case (not the norm).

ftplib.FTP.maxline = 16384    # This is double the default value

Just call that line at any point before you start the file transfer.

like image 4
Hans Goldman Avatar answered Nov 10 '22 10:11

Hans Goldman