With ftplib module, we can upload, download files, monitor or change directories in remote devices with FTP connection.


Import modules

import ftplib
import os


FTP Connection information

ftp = ftplib.FTP('129.9.0.102', 'root', 'Test1234.') 


Run "dir" command in current folder   

ftp.dir()                                                  


Choose file in local to upload

upload = open("Device_List.txt", 'rb')                                          


File upload from PC to device

ftp.storbinary('STOR %s' % os.path.basename("Device_Lis12t.txt"), upload, 1024)   


Create new directory

 ftp.mkd('newfile3')                                        


Change directory

 ftp.cwd("logfile")                                           


Run "dir" command in current folder

 ftp.dir()                                                    


Delete a file

 ftp.delete("test.cfg")                                      


Delete a folder

 ftp.rmd("newfile3")       

 


To login a device with FTP and file upload and download for peer device is as below.

import ftplib
import os

ftp = ftplib.FTP('129.9.0.102', 'root', 'Test1234.')        
ftp.dir()                                                  
print("..............1............................")

upload = open("Device_List.txt", 'rb')                                          
ftp.storbinary('STOR %s' % os.path.basename("Device_Lis12t.txt"), upload, 1024)