cmd – An FTP transfer command like STOR and RETR.
rest – A location in the file from where the transfer has to be resumed. The default value is None.
A tuple containing the socket and the expected size of the data.
- The method ntransfercmd() executes a transfer command like STOR, RETR and others.
- Using ntransfercmd() files can be downloaded from or uploaded to an FTP server in either binary mode or text mode.
- The file transfer can be done in either active mode or passive mode.
- The method is similar to transfercmd() but differs in return value. It returns a tuple containing a socket and the expected size of data.
# Example Python Program to upload a file from an FTP client
# in active mode
from ftplib import FTP
# Instantiate an FTP object
ftpObject = FTP()
# Connect to the FTP server
# Enable active mode
# Login to the FTP server
serverResponse = ftpObject.login();
# Change the working directory(if required)
# Initiate a file upload to the server in active mode
downloadCommand = "STOR 1KB.zip";
# Tell the server that forthcoming file transfer is in binary mode
serverResponse = ftpObject.sendcmd('TYPE I');
with open('512KB.zip', 'rb') as fd:
with ftpObject.ntransfercmd(downloadCommand) as dataConnection:
buffer = fd.read(1024);
if buffer == b'':
230 Login successful.
200 Switching to Binary mode.
226 Transfer complete.