Use Python to manipulate FTP uploads and downloads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.