前言
經過前面兩個小節的介紹,我們已經完成了MySQL數據庫的搭建和數據庫操作的事宜。 在本小節中,我們需要完成的任務是:使用python從FTP服務其上面獲取文本文件。
搭建測試FTP服務器
LZ的測試環境是在 Windows2012 (實體機上的操作系統) + Ubuntu 16.04 (虛擬機)。 為了簡單起見,我們就將FTP服務器搭建在 Windows 系統上面。開發和測試在 Ubuntu 系統上面。
1. 打開FTP設置 (Controlpanel -> Turn windows features on or off -> Add roles and features -> Server roles)
如上圖所示,勾選上項目后,點擊 “Next”, 系統會自動完成安裝。
2. 打開IIS, 設置FTP服務器
2.1 新建FTP Site: Site -> Add FTP Site 填寫FTP服務器的名字和物理路徑(實際文件存放在硬盤上位置), 完成后點擊 Next
2.2 設置FTP地址以及SSL (LZ這里選擇的是 No SSL, 當然也可以根據自己的需要設置);完成后點擊 Next
2.3 設置用戶以及使用權限
請注意: 這里的用戶 SPCAdmin 是LZ登錄系統的用戶名。 我們也可指定其他用戶來訪問FTP,但是必須保證用戶存在(需要在創建FTP之前先創建好)
2.4 驗證FTP服務器,打開瀏覽器,輸入地址和端口號
按照提示輸入正確的用戶名密碼后,我們就可以看到我們放到FTP服務器上的文件啦
python編程訪問FTP
我們將其封裝成一個python的文件 FTPUtil.py; 整體代碼如下
# -*- coding: utf-8 -*- #from ctypes import * import os #import sys import ftplib class FTPUtil: 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 = FTPUtil('10.137.185.88') ftp.Login('SPCAdmin', 'Siemens@2017') ftp.DownLoadFile('AHHFCH-sun-2018042706_local.txt', 'AHHFCH-sun-2018042706.txt') # ftp.DownLoadFileTree('del', '/del1') # ok # ftp.UpLoadFileTree('del', "/del1") ftp.close() print "ok!"
參考
FTP服務器搭建 : https://blog.csdn.net/exlsunshine/article/details/29181465
Python FTP: http://www.cnblogs.com/kaituorensheng/p/4480512.html