The Ftplib module, which is installed by default in Python, defines the FTP class , where functions are limited and can be used to implement a simple FTP client for uploading or downloading files, and the functions are listed below
FTP Login ConnectionFrom FtplibImport FTP#Load FTP module ftp=ftp ()#Set Variable Ftp.set_debuglevel (2)#Open Debug Level 2, Show details Ftp.connect ("Ip","Port")#The connected FTP sever and Port Ftp.login ("User","Password")#User name of the connection, passwordPrint Ftp.getwelcome ()#Print out welcome information Ftp.cmd ("Xxx/xxx")#Go to remote directory bufsize=1024#Set the buffer size Filename="Filename.txt"#The file that needs to be downloaded File_handle=open (filename,"Wb"). Write#Open the file locally in write mode Ftp.retrbinaly ("RETR filename.txt", File_handle,bufsize)#Receiving files on the server and writing to local files ftp.set_debuglevel (0)#Turn off debug mode Ftp.quit ()#Exit FTPFTP-related command operation FTP.CWD (pathname)#Set the path of the FTP current operation Ftp.dir ()#Displays all directory information under directory Ftp.nlst ()#Get the file under directory FTP.MKD (pathname)# New remote Directory Ftp.pwd () # Returns the current location Ftp.rmd (dirname) # Delete remote directory ftp.delete (filename) # Delete the remote file Ftp.rename (FromName, ToName)# FromName Modify the name to ToName. Ftp.storbinaly ("STOR filename.txt", file_handel,bufsize) # upload destination file ftp.retrbinary (" RETR filename.txt", file_handel,bufsize) # Download ftp file
The difference between ftp.quit () and Ftp.close ()
- Ftp.quit (): Sends the QUIT command to the server and shuts down the connection. This is a more "moderated" shutdown connection, but throws an exception if the server returns an error to the QUIT command.
- Ftp.close (): A unilateral shutdown of a connection should not be used after a closed connection, for example, after Ftp.quit () is not applied.
Example: Downloading and uploading Files
#Coding:utf-8From FtplibImportFtpImportTimeImportTarfileImportOs#!/usr/bin/python#-*-Coding:utf-8-*-From FtplibImportFtpDefFtpconnect (host, username, password): FTP =FTP ()#Ftp.set_debuglevel (2) Ftp.connect (host, 21) Ftp.login (username, password)ReturnFtp#Download files from FTPDefDownloadFile (FTP, RemotePath, localpath): bufsize = 1024fp = open (LocalPath,‘Wb‘) Ftp.retrbinary (‘RETR' +RemotePath, Fp.write, bufsize) ftp.set_debuglevel (0) Fp.close ()#Upload files from local to FTPDefUploadFile (FTP, RemotePath, localpath): bufsize = 1024fp = open (LocalPath,‘Rb‘) Ftp.storbinary (‘STOR' +RemotePath, FP, bufsize) ftp.set_debuglevel (0) Fp.close ()If__name__ = ="__main__": FTP = Ftpconnect ("113.105.139.xxx","ftp***","guest***") DownloadFile (FTP, "faint.mp4", "c:/users/administrator/desktop/test.mp4 ") # Call the local player to play the downloaded video os.system ( ' start "C:\Program files\ Windows Media Player\wmplayer.exe "" C:/users/administrator/desktop/test.mp4 "" ) UploadFile (FTP, "c:/users/ Administrator/desktop/test.mp4" test.mp4 ") Ftp.quit ()
Use Python to manipulate FTP uploads and downloads