Method Name:
storbinary
Method Signature:
storbinary(cmd, fp, blocksize, callback, rest)
Parameters:
cmd – The FTP command to be used.
fp – The file object. Typically obtained through the built-in function open().
blocksize - The maximum size of the to be read and sent through the network. The default size is 8192 bytes.
callback- The callback function. If provided, this function is called for every blocksize bytes sent.
rest – The location in the file from which the upload has to be continued.
Return Type:
A success message “226 Transfer complete“ or a failure message like “553 Could not create file”.
Overview:
- The method storbinary() initiates a file transfer from an FTP client to a an FTP server using the FTP command STOR.
- The file transfer is done in binary mode. The method internally sends a “TYPE I” command to the FTP server, before sending a STOR command, meaning the next transfer is a binary image.
- In binary mode, no translation of CR LF characters is done. This mode of transfer is preferred for binary files. To transfer text files, storlines() method can be used.
Example:
# Example Python program to upload a file to an FTP server # in binary mode from ftplib import FTP
# Create an FTP object and connect to the server # as anonymous user ftpObject = FTP(host="anftpserverhost"); print(ftpObject.getwelcome());
# Login to the server ftpResponseMessage = ftpObject.login(); print(ftpResponseMessage);
# Change to the required working directory ftpResponseMessage = ftpObject.cwd("/upload"); print(ftpResponseMessage);
# Open the file in binary mode fileObject = open("1KB.zip", "rb"); file2BeSavedAs = "OneKB.zip"
ftpCommand = "STOR %s"%file2BeSavedAs;
# Transfer the file in binary mode ftpResponseMessage = ftpObject.storbinary(ftpCommand, fp=fileObject); print(ftpResponseMessage); |
Output:
220 (vsFTPd 3.0.3) 230 Login successful. 250 Directory successfully changed. 226 Transfer complete. |