Python installed by default ftplib module defines the FTP class , wherein the function is limited, may be used to achieve a simple ftp client, for uploading or downloading files, functions listed below
Log ftp connections from ftplib Import the FTP # load modules ftp ftp = the FTP () # set the variable ftp.set_debuglevel (2) # open the debug level 2, the detailed information ftp.connect ( " IP " , " Port " ) # connection ftp sever and port ftp.login ( " the user " , " password " ) # username connection, password Print ftp.getwelcome () # prints out a welcome message ftp.cmd ( " xxx / xxx ") # To enter the remote directory bufsize = 1024 # Set the buffer size filename = " filename.txt " # file to be downloaded file_handle = Open (filename, " wb " ) .write # to write mode opens the file in the local ftp.retrbinaly ( " RETR filename.txt " , file_handle, bufsize) # on the receiving server file and write local files ftp.set_debuglevel (0) # turn off debugging mode ftp.quit () # exit the FTP the FTP commands related to the operation ftp.cwd (pathname) # set current path of the FTP operation ftp.dir () # display all directory information in the directory ftp.nlst () # files in the directory to obtain ftp.mkd (pathname) # Create remote directory ftp.pwd () # returns the current location ftp.rmd (dirname) # delete remote directory ftp.delete (filename) # delete remote files ftp.rename (fromname, ToName) # the fromname change the name to toname. ftp.storbinaly ( " STOR filename.txt " , file_handel, bufsize) # upload target file ftp.retrbinary ( " RETR filename.txt ", file_handel, bufsize) # download FTP file
Difference FTP.quit () and FTP.close () of
- FTP.quit (): QUIT command and closes off the connection to the server. This is a "detente" close connection, but if the server returns an error to the QUIT command will throw an exception.
- After closing off the unilateral connection should not be used after a connection has been closed, for example, is not applied in FTP.quit (): FTP.close ()
# coding: utf-8 from ftplib import FTP import time import tarfile import os # !/usr/bin/python # -*- coding: utf-8 -*- from ftplib import FTP def ftpconnect(host, username, password): ftp = FTP() # ftp.set_debuglevel(2) ftp.connect(host, 21) ftp.login(username, password) return ftp #从ftp下载文件 def downloadfile(ftp, remotepath, localpath): bufsize = 1024 fp = open(localpath, 'wb') ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize) ftp.set_debuglevel(0) fp.close() #从本地上传文件到ftp def uploadfile(ftp, remotepath, localpath): bufsize = 1024 fp = 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") #调用本地播放器播放下载的视频 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()