標籤:
Python中預設安裝的ftplib模組定義了FTP類,其中函數有限,可用來實現簡單的ftp用戶端,用於上傳或下載檔案,函數列舉如下
ftp登陸串連from ftplib import FTP #載入ftp模組ftp=FTP() #設定變數ftp.set_debuglevel(2) #開啟調試層級2,顯示詳細資料ftp.connect("IP","port") #串連的ftp sever和連接埠ftp.login("user","password") #串連的使用者名稱,密碼print ftp.getwelcome() #列印出歡迎資訊ftp.cmd("xxx/xxx") #進入遠程目錄bufsize=1024 #設定的緩衝區大小filename="filename.txt" #需要下載的檔案file_handle=open(filename,"wb").write #以寫入模式在本地開啟檔案ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) #接收伺服器上檔案並寫入本地檔案ftp.set_debuglevel(0) #關閉偵錯模式ftp.quit() #退出ftpftp相關命令操作ftp.cwd(pathname) #設定FTP當前操作的路徑ftp.dir() #顯示目錄下檔案資訊ftp.nlst() #擷取目錄下的檔案ftp.mkd(pathname) #建立遠程目錄ftp.pwd() #返回當前所在位置ftp.rmd(dirname) #刪除遠程目錄ftp.delete(filename) #刪除遠程檔案ftp.rename(fromname, toname)#將fromname修改名稱為toname。ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #上傳目標檔案ftp.retrbinary("RETR filename.txt",file_handel,bufsize) #下載FTP檔案
例1:下載、上傳檔案
#coding: utf-8from ftplib import FTPimport timeimport tarfile#!/usr/bin/python#-*- coding: utf-8 -*-from ftplib import FTPdef ftpconnect(host, username, password): ftp = FTP() #ftp.set_debuglevel(2) #開啟調試層級2,顯示詳細資料 ftp.connect(host, 21) #串連 ftp.login(username, password) #登入,如果匿名登入則用空串代替即可 return 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() #關閉檔案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("******", "***", "***") downloadfile(ftp, "***", "***") uploadfile(ftp, "***", "***") ftp.quit()
例2:上傳、下載檔案/目錄
#coding:utf-8from ctypes import *import osimport sysimport ftplibclass myFtp: ftp = ftplib.FTP() bIsDir = False path = "" def __init__(self, host, port=‘21‘): #self.ftp.set_debuglevel(2) #開啟調試層級2,顯示詳細資料 #self.ftp.set_pasv(0) #0主動模式 1 #被動模式 self.ftp.connect( host, port ) def Login(self, user, passwd): self.ftp.login( user, passwd ) print self.ftp.welcome def DownLoadFile(self, LocalFile, RemoteFile): file_handler = open( LocalFile, ‘wb‘ ) self.ftp.retrbinary( "RETR %s" %( RemoteFile ), file_handler.write ) file_handler.close() return True def UpLoadFile(self, LocalFile, RemoteFile): if os.path.isfile( LocalFile ) == False: return False file_handler = open(LocalFile, "rb") self.ftp.storbinary(‘STOR %s‘%RemoteFile, file_handler, 4096) file_handler.close() return True def UpLoadFileTree(self, LocalDir, RemoteDir): if os.path.isdir(LocalDir) == False: return False print "LocalDir:", LocalDir LocalNames = os.listdir(LocalDir) print "list:", LocalNames print RemoteDir self.ftp.cwd( RemoteDir ) for Local in LocalNames: src = os.path.join( LocalDir, Local) if os.path.isdir( src ): self.UpLoadFileTree( src, Local ) else: self.UpLoadFile( src, Local ) self.ftp.cwd( ".." ) return def DownLoadFileTree(self, LocalDir, RemoteDir): print "remoteDir:", RemoteDir if os.path.isdir( LocalDir ) == False: os.makedirs( LocalDir ) self.ftp.cwd( RemoteDir ) RemoteNames = self.ftp.nlst() print "RemoteNames", RemoteNames print self.ftp.nlst("/del1") for file in RemoteNames: Local = os.path.join( LocalDir, file ) if self.isDir( file ): self.DownLoadFileTree( Local, file ) else: self.DownLoadFile( Local, file ) self.ftp.cwd( ".." ) return def show(self, list): result = list.lower().split( " " ) if self.path in result and "<dir>" in result: self.bIsDir = True def isDir(self, path): self.bIsDir = False self.path = path #this ues callback function ,that will change bIsDir value self.ftp.retrlines( ‘LIST‘, self.show ) return self.bIsDir def close(self): self.ftp.quit()if __name__ == "__main__": ftp = myFtp(‘*****‘) ftp.Login(‘***‘,‘***‘) ftp.DownLoadFileTree(‘del‘, ‘/del1‘)#ok ftp.UpLoadFileTree(‘del‘, "/del1" ) ftp.close() print "ok!"
注:目錄內為檔案,若為目錄則無法傳輸
python ftplib模組