一、程序說明
1.1 程序實現關鍵點
python實現ftp客戶端,主要會遇到以下四個問題:
第一個問題是使用什么包實現----我們這里是使用標准庫中的ftplib
第二個問題是怎么連接登錄ftp服務器----如果是ssh那么直接使用connect函數就直接完成連接和登錄兩項工作,而ftp要先用connect連接然后再用login登錄
第三個問題是怎么實現ftp操作----怎么實現ftp操作這個問題有點麻煩,我們平時使用ftp命令登錄后就可以輸入下圖中的命令進行ftp操作,對於其中少數一些命令ftplib確實支持通過sendcmd方法將命令傳過去然后服務端就執行然后返回執行結果,但對於大多數命令ftplib都是通過一個方法去實現的(比如cd命令對應cwd()方法,mkdir命令對應mkd()方法等,參見1.2)
第四個問題是怎么實現文件的上傳下載----文件上傳下載其實也是ftp操作的一部份,如上面所說ftplib基本是每個命令都使用一個方法去實現,而對於上傳的put命令對應的就是storbinary方法,對於下載的get命令對應的就是retrbinary方法。具體參數和用法可參看下邊程序源代碼

1.2 常用ftp命令與函數對應關系
全部方法及方法參數解析可參看官方文檔
| ftp命令 | 對應的FTP對象方法 | 備注 |
| ls | nlst([pathname]) | Return a list of file names as returned by the NLST command. |
| dir | dir([pathname]) | Produce a directory listing as returned by the LIST command, printing it to standard output. |
| rename | rename(fromname, toname) | Rename file fromname on the server to toname. |
| delete | delete(filename) | Remove the file named filename from the server. |
| cd | cwd(pathname) | Set the current directory on the server. |
| mkdir | mkd(pathname) | Create a new directory on the server. |
| pwd | pwd() | Return the pathname of the current directory on the server. |
| rmdir | rmd(dirname) | Remove the directory named dirname on the server. |
| size(filename) | Request the size of the file named filename on the server. | |
| quit | quit() | Send a QUIT command to the server and close the connection. |
| close | close() | Close the connection unilaterally. |
| put | storbinary(cmd, fp, blocksize=8192, callback=None, rest=None) | Store a file in binary transfer mode. |
| get | retrbinary(cmd, callback, blocksize=8192, rest=None) | Retrieve a file in binary transfer mode. |
1.3 程序截圖
程序執行結果截圖:

二、程序源代碼
import logging from ftplib import FTP class MyFtp(): def __init__(self): self.ftp_client = FTP() # 些函數實現ftp登錄 def ftp_login(self,host_ip,username,password): try: self.ftp_client.connect(host_ip,port=21,timeout=10) except : logging.warning('network connect time out') return 1001 try: self.ftp_client.login(user=username, passwd=password) except: logging.warning('username or password error') return 1002 return 1000 # 此函數執行ftp命令,並打印命令執行結果 def execute_some_command(self): # 通運sendcmd方法形式執行pwd命令,為使用形式統一起見不推薦使用此種形式,而且其實大多數命令都是支持這種形式的 command_result = self.ftp_client.sendcmd('pwd') logging.warning('command_result:%s'% command_result) # 通過直接使用pwd方法執行pwd命令,推薦統一使用此種形式 command_result = self.ftp_client.pwd() logging.warning('command_result:%s' % command_result) # 上傳文件;'stor ftp_client.py'告訴服務端將上傳的文件保存為ftp_client.py,open()是以二進制讀方式打開本地要上傳的文件 command_result = self.ftp_client.storbinary('stor ftp_client.py',open("ftp_client.py",'rb')) logging.warning('command_result:%s' % command_result) # 下載文件;'retr .bash_profile'告訴服務端要下載服務端當前目錄下的.bash_profile文件,open()是以二進制寫方式打開本地要存成的文件 command_result = self.ftp_client.retrbinary('retr .bash_profile', open('local_bash_profile', 'wb').write) logging.warning('command_result:%s' % command_result) # 此函數實現退出ftp會話 def ftp_logout(self): logging.warning('now will disconnect with server') self.ftp_client.close() if __name__ == '__main__': # 要連接的主機ip host_ip = '192.68.220.128' # 用戶名 username = 'ls' # 密碼 password = 'abcd1234' # 實例化 my_ftp = MyFtp() # 如果登錄成功則執行命令,然后退出 if my_ftp.ftp_login(host_ip,username,password) == 1000: logging.warning('login success , now will execute some command') my_ftp.execute_some_command() my_ftp.ftp_logout()
參考:
https://docs.python.org/3/library/ftplib.html
pwd()
