The ftplib module has the FTP class and can perform various upload and download operations under the FTP protocol. The following describes how to use the ftplib in the FTP communication module in Python: the default ftplib module installed in Python defines the FTP class. the function is limited and can be used to upload or download files.
For the FTP workflow and basic operations, refer to RFC959.
Ftp login connection
From ftplib import FTP # load ftp module ftp = FTP () # set the variable ftp. set_debuglevel (2) # enable debug level 2 and display the ftp details. connect ("IP", "port") # connect to the ftp server and port ftp. login ("user", "password") # connect to the user name and password print ftp. getwelcome () # print out the welcome information ftp. cmd ("xxx/xxx") # Change remote directory bufsize = 1024 # set the buffer size filename = "filename.txt" # file file_handle = open (filename, "wb "). write # Open the ftp file locally in write mode. retrbinaly ("RETR filename.txt", file_handle, bufsize) # receives files from the server and writes them to the local file ftp. set_debuglevel (0) # Disable the debugging mode ftp. quit # exit ftp
Ftp Commands
Ftp. cwd (pathname) # set the FTP current operation path. dir () # display the file information in the directory ftp. nlst () # obtain the file ftp in the directory. mkd (pathname) # Create a remote directory ftp. pwd () # return the current ftp location. rmd (dirname) # Delete the remote directory ftp. delete (filename) # delete a remote file ftp. rename (fromname, toname) # Change fromname to toname. Ftp. storbinaly ("STOR filename.txt", file_handel, bufsize) # upload the target file ftp. retrbinary ("RETR filename.txt", file_handel, bufsize) # Download the FTP file
Instance
Upload code:
Import socketfrom ftplib import FTPftp_server = 'XX. xx. xx. xx 'ftp _ user = 'xxxxx' ftp _ password = 'xxxxx' ftp _ backup_dir = 'backup 'newday = date. today () # obtain today's date oldday = date. today ()-timedelta (5) # obtain the date newfile = '/home/backup/' + 'backup _ data _ '+ str (newday. year) + '. '+ str (newday. month) + '. '+ str (newday. day) + '.zip '# Name of the backup file (absolute path) oldfile ='/home/backup/'+ 'backup _ data _' + str (oldday. year) + '. '+ str (Oldday. month) + '. '+ str (oldday. day) + '.zip '# file name backed up five days ago (absolute path) def upload (): socket. setdefatimetimeout (60) # set the timeout FTP time to 60 seconds ftp = FTP (ftp_server) print ("login ftp... ") try: ftp. login (ftp_user, ftp_password) print (ftp. getwelcome () # obtain the welcome information. try: if ftp_backup_dir in ftp. nlst (): print ("found backup folder in ftp server, upload processing. ") else: print (" don't found backup folder in ftp server, try Build it. ") ftp. mkd (ftp_backup_dir) failed t: print (" the folder "+ ftp_backup_dir +" doesn't exits and can't be create! ") Sys. exit () failed t: print ("ftp login failed. exit. ") sys. exit () ftp. cwd (ftp_backup_dir) # set the FTP path print ("upload data... ") try: ftp. storbinary ('stor' + OS. path. basename (newfile), open (newfile, 'RB'), 1024) # upload the backup file upload T: print ("upload failed. check your permission. ") print (" delte old file... ") try: ftp. delete (OS. path. basename (oldfile) # Delete the backup file snapshot t: print ("the old file in ftp doesn't exists, jumped. ") print (" ftp upload successful. exit... ") ftp. quit () if _ name __= = '_ main _': upload ()