Method Name:
retrbinary
Method Signature:
retrbinary(cmd, callback, blocksize=8192, rest=None)
Parameters:
cmd – The FTP command to be sent to retrieve the file.
callback – For each block of the data received from the FTP server the callback function is called. This callback function can be used for processing the data received. For example, the callback can be used for writing the received blocks into a file, or insert the data into a database like MySQL or rendering it as it is received.
blocksize – maximum number of bytes to read from the underlying socket connection
rest – a string literal containing the offset location of the file from which the FTP server should resume the transfer
Overview:
- FTP, the File Transfer Protocol has two modes for data transfer: ASCII Mode and Binary Mode.
- ASCII mode is used for transferring files in text format. This mode exists as the CR and linefeed characters are different in different operating systems and FTP takes care of the proper translation of them while using the ASCII mode.
- Binary mode is used for transferring binary files. To transfer binary files ASCII mode should not be used. Doing so could corrupt the files.
- The method retrbinary() of the FTP class retrieves a file from the server to the local system in binary mode. The method accepts a retrieval command such as RETR, and a callback function along with other paramaters.
- The method retrbinary() supports resumed FTP transfers. Using the parameter rest the position from which the file transfer has to be made can be specified.
Example:
from ftplib import FTP
serverName = 'somepublicftpserver.com'
# Connect to the FTP server ftpConnection = FTP(serverName);
# Do an anonymous login ftpConnection.login();
# Retrieve the file in binary mode ftpConnection.retrbinary("RETR 1KB.zip", open('1KB.zip', 'wb').write) |
Output:
>ls 1KB* 1KB.zip |