Background: FTP operation is required, involving upload and download operations
fromFtplibImportFTP FTP= FTP (host= "ftp hostname", user= ' login username ', passwd= ' login password ') # Connect to FTPFtp.set_debuglevel (2)#turn on debug Level 2, show more information defDownload (filename,bufsize=1024):#bufsize The buffer size setFile_handle = open (filename,"WB"). Write#to open a file locally in write modeFtp.retrbinary ("RETR%s"%filename, File_handle, bufsize)#receiving files on the server and writing to local files defUpload (filename,bufsize=1024): File_handle2= open (filename,"RB")#to open a local file in read-only modeFtp.storbinary ('STOR%s'%filename, File_handle2, bufsize)#upload local filename to serverftp.getwelcome ()#Print out a welcome messageFTP.CWD ('Directory name')#go to remote directoryFTP.RMD (' Directory name'# Delete the directory on FTPFTP.MKD ('Directory name')#Create a directory on FTPFtp.pwd ()#Print the current FTP directoryFtp.dir ()#Print all files in the current directory on FTPftp.quit ()#Exit FTP
Specific Description:1.FTP = FTP (host=
"ftp hostname",user=
' login username ', passwd=' login password
') FTP () is actually called. Login to log in 2.download downloading files from FTPlocal files need to be opened in WB write mode and then downloaded via the Retrinary () method 3.upload uploading local files to FTP serverThe local file is opened in read-only mode and then uploaded via the Storbinary () method 4. Some other common methodsgetwelcome (): Print the FTP welcome, set on the FTP servercwd (' Directory name '): Enter a directory on the FTP server and switch directoriesRMD (' Directory name '): Delete directoryMKD (' Directory name '): Create directoryftp.pwd (): Print the current directorydir (): Print all files in the current directoryquit () or close (): Close FTP connection Note: If you want to implement the upload directory or upload files according to regular match, you need to write the method separately
Python operates on FTP