Method Name:
ntransfercmd
Method Signature:
ntransfercmd(cmd, rest=None)
Parameters:
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.
Return Value:
A tuple containing the socket and the expected size of the data.
Overview:
- 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:
# 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 ftpObject.connect(host="speedtest.tele2.net");
# Enable active mode ftpObject.set_pasv(False);
# Login to the FTP server serverResponse = ftpObject.login(); print(serverResponse);
# Change the working directory(if required) ftpObject.cwd("/upload");
# 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'); print(serverResponse);
with open('512KB.zip', 'rb') as fd: with ftpObject.ntransfercmd(downloadCommand)[0] as dataConnection: while 1: buffer = fd.read(1024); if buffer == b'': break; dataConnection.sendall(buffer); print(ftpObject.voidresp()); |
Output:
230 Login successful. 200 Switching to Binary mode. 226 Transfer complete. |