FTP protocol : A fairly common protocol (before 2010), which is preferred by many programmers for file storage for sharing between systems.
While it may seem outdated at present, but there are still some “old” generations still in use and “who may be surprised”, one day we will have to work with it.
I have encountered such a case, the client’s system is Windows Server, their current plan to share files is to use FTP Server.
My task is to access the customer’s server, move to the exact folder they specified, search for any newly created files (pushed by the customer), download the file. The processor then accesses the ftp server and uploads the file to a directory they specify, sometimes creating a new directory to execute.
I solved the above problem using python’s ftplib library. Please see my partial listing code below.
Build a ftp server
To continue with the article, if you want to perform the demo according to my code, you can create your own ftp-server following the instructions.
To build an FTP Server, on Windows is very easy, use FileZilla Server: https://quantrimang.com/huong-dan-thiet-lap-ftp-server-ca-nhan-bang-filezilla-84531
For Ubuntu, follow the instructions (remember to select Authenticated): https://ubuntu.com/server/docs/service-ftp
Features of FTP Server:
- File storage
- User / password management
- Assign access rights
- Provide connection through any port (default: 21):
- Connect anonymously.
- Connection identifier: user / password.
FTP Client:
- Program on the user’s computer, used to connect to the FTP Server
Building FTP-CLIENT using ftplib library
In Python, if you want to connect to FTP, use the ftplib library, this is the default library in the system (available after installing python) so no need to install more.
Initiate connection
1 2 3 4 5 6 7 8 9 10 11 | <span class="token keyword">from</span> ftplib <span class="token keyword">import</span> FTP ftp_ip <span class="token operator">=</span> <span class="token string">"your-host-ip"</span> ftp_usr <span class="token operator">=</span> <span class="token string">"your-username"</span> ftp_pwd <span class="token operator">=</span> <span class="token string">"your-password"</span> ftp_client <span class="token operator">=</span> FTP <span class="token punctuation">(</span> ftp_ip <span class="token punctuation">)</span> ftp_client <span class="token punctuation">.</span> login <span class="token punctuation">(</span> user <span class="token operator">=</span> ftp_usr <span class="token punctuation">,</span> passwd <span class="token operator">=</span> ftp_pwd <span class="token punctuation">)</span> |
After successful login, the data returns:
‘230 Logged on’
See ftp-server specifications
1 2 | ftp_client <span class="token punctuation">.</span> welcome |
‘220-FileZilla Server 0.9.60 beta n220-written by Tim Kosse ( [email protected] ) n220 Please visit https://filezilla-project.org/ ‘
The commands related to directory / file.
Display the current directory
1 2 3 | ftp_client <span class="token punctuation">.</span> pwd <span class="token punctuation">(</span> <span class="token punctuation">)</span> |
‘/’
Depending on the configuration & authorization of the server according to each login user, the default is to the ftp-server root directory, whose user after logging in will be assigned to a deep internal directory and does not have permission to translate. move out to external folders.
Lists directories / files in the directory connected to
1 2 | <span class="token keyword">print</span> <span class="token punctuation">(</span> ftp_client <span class="token punctuation">.</span> retrlines <span class="token punctuation">(</span> <span class="token string">'LIST'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> |
123456 -rw-r--r-- 1 ftp ftp 11234 May 09 21:39 Doc1.docxdrwxr-xr-x 1 ftp ftp 0 May 09 23:21 Folder1drwxr-xr-x 1 ftp ftp 0 May 09 21:26 Folder2-rw-r--r-- 1 ftp ftp 1085 May 09 21:49 sample.ipynb226 Successfully transferred "/"
Move to another folder from the parent directory:
1 2 | ftp_client <span class="token punctuation">.</span> cwd <span class="token punctuation">(</span> <span class="token string">"Folder1"</span> <span class="token punctuation">)</span> |
12 '250 CWD successful. "/Folder1" is current directory.'
Note: Only move up to 1 subfolder level
Move to the previous folder.
1 2 3 | ftp_client <span class="token punctuation">.</span> cwd <span class="token punctuation">(</span> <span class="token string">".."</span> <span class="token punctuation">)</span> |
‘200 CDUP successful. “/” is current directory. ‘
Create a new folder
1 2 3 4 | ftp_client <span class="token punctuation">.</span> mkd <span class="token punctuation">(</span> <span class="token string">"NewFolder"</span> <span class="token punctuation">)</span> |
12 '/NewFolder'
Delete directory
1 2 3 | ftp_client <span class="token punctuation">.</span> rmd <span class="token punctuation">(</span> <span class="token string">"NewFolder"</span> <span class="token punctuation">)</span> |
12 '250 Directory deleted successfully'
File related command
Check the file size
1 2 | ftp_client <span class="token punctuation">.</span> size <span class="token punctuation">(</span> <span class="token string">"Doc1.docx"</span> <span class="token punctuation">)</span> |
12 11234
The unit of capacity here is Byte
Delete the file
1 2 3 4 | ftp_client <span class="token punctuation">.</span> delete <span class="token punctuation">(</span> <span class="token string">"Doc1.docx"</span> <span class="token punctuation">)</span> <span class="token keyword">print</span> <span class="token punctuation">(</span> ftp_client <span class="token punctuation">.</span> retrlines <span class="token punctuation">(</span> <span class="token string">'LIST'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> |
12345 drwxr-xr-x 1 ftp ftp 0 May 09 23:21 Folder1drwxr-xr-x 1 ftp ftp 0 May 09 21:26 Folder2-rw-r--r-- 1 ftp ftp 1085 May 09 21:49 sample.ipynb226 Successfully transferred "/"
Upload the file.
- Conditions upload file from the client must be in the same or deeper than the location where the file runs ftp-client (file code .python) or must point the correct path to the file location.
- Want to upload to a certain folder, then make a connection to that folder on the server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <span class="token keyword">from</span> ftplib <span class="token keyword">import</span> FTP ftp_client <span class="token operator">=</span> FTP <span class="token punctuation">(</span> ftp_ip <span class="token punctuation">)</span> ftp_client <span class="token punctuation">.</span> login <span class="token punctuation">(</span> user <span class="token operator">=</span> ftp_usr <span class="token punctuation">,</span> passwd <span class="token operator">=</span> ftp_pwd <span class="token punctuation">)</span> <span class="token comment"># di chuyển đến thư mục Folder1</span> ftp_client <span class="token punctuation">.</span> cwd <span class="token punctuation">(</span> <span class="token string">"Folder1"</span> <span class="token punctuation">)</span> <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">"before uploadn"</span> <span class="token punctuation">,</span> ftp_client <span class="token punctuation">.</span> retrlines <span class="token punctuation">(</span> <span class="token string">"LIST"</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token comment"># read file to send to byte</span> file_stream <span class="token operator">=</span> <span class="token builtin">open</span> <span class="token punctuation">(</span> <span class="token string">"sample_9_5.ipynb"</span> <span class="token punctuation">,</span> <span class="token string">"rb"</span> <span class="token punctuation">)</span> <span class="token comment"># send the file </span> ftp_client <span class="token punctuation">.</span> storbinary <span class="token punctuation">(</span> <span class="token string">"{CMD} {FileName}"</span> <span class="token punctuation">.</span> <span class="token builtin">format</span> <span class="token punctuation">(</span> CMD <span class="token operator">=</span> <span class="token string">"STOR"</span> <span class="token punctuation">,</span> FileName <span class="token operator">=</span> <span class="token string">"sample_9_5.ipynb"</span> <span class="token punctuation">)</span> <span class="token punctuation">,</span> file_stream <span class="token punctuation">)</span> file_stream <span class="token punctuation">.</span> close <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">"after uploadn"</span> <span class="token punctuation">,</span> ftp_client <span class="token punctuation">.</span> retrlines <span class="token punctuation">(</span> <span class="token string">"LIST"</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> ftp_client <span class="token punctuation">.</span> close |
123456789101112 drwxr-xr-x 1 ftp ftp 0 May 09 21:31 Folder3-rw-r--r-- 1 ftp ftp 6174 May 09 21:32 New t.xlsx-rw-r--r-- 1 ftp ftp 1085 May 09 21:50 sample.ipynbbefore upload226 Successfully transferred "/Folder1"drwxr-xr-x 1 ftp ftp 0 May 09 21:31 Folder3-rw-r--r-- 1 ftp ftp 6174 May 09 21:32 New t.xlsx-rw-r--r-- 1 ftp ftp 1085 May 09 21:50 sample.ipynb-rw-r--r-- 1 ftp ftp 1085 May 10 18:59 sample_9_5.ipynbafter upload226 Successfully transferred "/Folder1"
Download the file
- Where to download, run the program file at that location.
- Connect to the FTP server and download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <span class="token keyword">from</span> ftplib <span class="token keyword">import</span> FTP ftp_client <span class="token operator">=</span> FTP <span class="token punctuation">(</span> ftp_ip <span class="token punctuation">)</span> ftp_client <span class="token punctuation">.</span> login <span class="token punctuation">(</span> user <span class="token operator">=</span> ftp_usr <span class="token punctuation">,</span> passwd <span class="token operator">=</span> ftp_pwd <span class="token punctuation">)</span> ftp_client <span class="token punctuation">.</span> cwd <span class="token punctuation">(</span> <span class="token string">"Folder1"</span> <span class="token punctuation">)</span> <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">"before uploadn"</span> <span class="token punctuation">,</span> ftp_client <span class="token punctuation">.</span> retrlines <span class="token punctuation">(</span> <span class="token string">"LIST"</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> file_path <span class="token operator">=</span> <span class="token string">"New t.xlsx"</span> file_name <span class="token operator">=</span> <span class="token string">"New t.xlsx"</span> file_stream <span class="token operator">=</span> <span class="token builtin">open</span> <span class="token punctuation">(</span> file_path <span class="token punctuation">,</span> <span class="token string">"wb"</span> <span class="token punctuation">)</span> <span class="token comment"># read file to send to byte</span> ftp_client <span class="token punctuation">.</span> retrbinary <span class="token punctuation">(</span> <span class="token string">'RETR {}'</span> <span class="token punctuation">.</span> <span class="token builtin">format</span> <span class="token punctuation">(</span> file_name <span class="token punctuation">)</span> <span class="token punctuation">,</span> file_stream <span class="token punctuation">.</span> write <span class="token punctuation">,</span> <span class="token number">1024</span> <span class="token punctuation">)</span> file_stream <span class="token punctuation">.</span> close <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">"Download OK"</span> <span class="token punctuation">)</span> ftp_client <span class="token punctuation">.</span> close |
12345678 drwxr-xr-x 1 ftp ftp 0 May 09 21:31 Folder3-rw-r--r-- 1 ftp ftp 6174 May 09 21:32 New t.xlsx-rw-r--r-- 1 ftp ftp 1085 May 09 21:50 sample.ipynb-rw-r--r-- 1 ftp ftp 1085 May 10 18:59 sample_9_5.ipynbbefore upload226 Successfully transferred "/Folder1"Download OK
Close the connection.
Close the connection when done downloading / uploading.
1 2 | ftp_client <span class="token punctuation">.</span> close |
<bound method ftp_client.close of <ftplib.FTP object at 0x000001F61AFE65F8 >>
In addition to the basic commands above, you can refer to the other commands of ftplib from the following link: https://docs.python.org/3/library/ftplib.html