Python FTP Commands














































Python FTP Commands



Python FTP Commands (ftplib)
In the last article we learnt about Directory Listing of a FTP server using the Python ftplib library.
We learnt What a Directory is and saw how a FTP directory looks like. We used the dir() method to produce the output which included the directory listing of the FTP server ("ftp.nluug.nl").

In this article we will learn about different FTP COMMANDS, their uses and their functionalities.

FTP Commands:
The FTP client sends commands to the FTP server, such as PWD or RETR. The ftplib library contains several methods that wrap these commands. The commands are send with the sendcmd() or voidcmd() methods.
Lets first see the Example Program and then we will see the use of the Commands and the functions used and their uses.  Code:

The example retrieves the current working directory by sending a PWD command directly and using the pwd() method.

Explanation:

1) We send the PWD command with the sendcmd() method.
2) This command sends a simple command string to the server and returns the response string.


1) The parse257() is a helper method that retrieves the directory from the returned string (wdir in the            example code), which also contains the status code.


1) Here we use the pwd() method to retrieve the current working directory.

Output:


Changing The Directory:
The cwd() method changes the current working directory.

Code:


The example uses the cmd() method to change to the site folder.

Output:

This example shows how we use the cmd() method to go from the root directory into the site folder of the FTP server "ftp.nluug.nl".

Conclusion: We learnt about the FTP Commands and the different methods available in the ftplib library which are used to retrieve directory and also move from one directory to other.
In the next article, We will learn about some other methods available to create a directory, text files and other operations available in the library.

Comments