基於python實現FTP文件上傳與下載(ftp&sftp協議)

前言: FTP(File Transfer Protocol),是文件傳輸協議的簡稱。用於Internet上的控制文件的雙向傳輸。同時,它也是一個應用程序(Application)。用戶能夠經過它把本身的PC機與世界各地全部運行FTP協議的服務器相連,訪問服務器上的大量程序和信息。若是用戶須要將文件從本身的計算機上發送到另外一臺計算機上,可以使用FTP上傳(upload)或(put)操做,而更多種的狀況是用戶使用FTP下載(download)或獲取(get)操做從FTP服務器上下載文件 在傳輸文件時咱們可能會選擇sftp和ftp兩種協議中的一種,二者的主要區別在於安全與傳輸速度,FTP傳輸數據的過程,他們在不一樣協議下的默認端口號是不一樣的,它有兩種傳輸模式:主動傳輸模式(PORT)和被動傳輸模式(PASSIVE,簡稱PASV),關於FTP相關內容這裏就不作詳細數明瞭,這裏將以python語言實現其功能python

一 、基於ftp協議 Python中默認安裝的ftplib模塊定義了FTP類,其中函數有限,可用來實現簡單的ftp客戶端,用於上傳或下載文件,函數列舉以下linux

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() #退出ftpwindows

ftp相關命令操做 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文件安全

#!/usr/bin/env python
#coding:utf-8

from ctypes import *
import os
import sys
import ftplib
import time

today = time.strftime('%Y%m%d',time.localtime(time.time()))
ip = '111.111.111.6'
username = 'ftpUserName' 
password = 'ftpPassWord'
filename = '203200189'+ today + 'A001.tar.gz'
src_file = '/ftpFilePath/'+filename

class myFtp:
    ftp = ftplib.FTP()
    ftp.set_pasv(False) 
    def __init__(self, host, port=21):
        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')
        print(file_handler)
        # self.ftp.retrbinary("RETR %s" % (RemoteFile), file_handler.write)#接收服務器上文件並寫入本地文件
        self.ftp.retrbinary('RETR ' + RemoteFile, file_handler.write)
        file_handler.close()
        return True

    def DownLoadFileTree(self, LocalDir, RemoteDir):  # 下載整個目錄下的文件
        print("remoteDir:", RemoteDir)
        if not os.path.exists(LocalDir):
            os.makedirs(LocalDir)
        self.ftp.cwd(RemoteDir)
        RemoteNames = self.ftp.nlst()
        print("RemoteNames", RemoteNames)
        for file in RemoteNames:
            Local = os.path.join(LocalDir, file)
            print(self.ftp.nlst(file))
            if file.find(".") == -1:
                if not os.path.exists(Local):
                    os.makedirs(Local)
                self.DownLoadFileTree(Local, file)
            else:
                self.DownLoadFile(Local, file)
        self.ftp.cwd("..")
        return True

    #從本地上傳文件到ftp
    def uploadfile(self, remotepath, localpath):
      bufsize = 1024
      fp = open(localpath, 'rb')
      ftp.storbinary('STOR ' + remotepath, fp, bufsize)
      ftp.set_debuglevel(0)
      fp.close() 

    def close(self):
        self.ftp.quit()
 
 
if __name__ == "__main__":
    ftp = myFtp(ip)
    ftp.Login(username, password)
    ftp.DownLoadFile(filename,src_file )
    #ftp.DownLoadFileTree('.', '/cteidate/')

    ftp.close()
    print("ok!")
複製代碼

二 、基於sftp協議 在Python中可使用paramiko模塊中的sftp登錄遠程主機,實現上傳和下載功能。bash

#!/usr/bin/python
# coding=utf-8

import paramiko
import os

def sftp_upload(host,port,username,password,local,remote):
    sf = paramiko.Transport((host,port))
    sf.connect(username = username,password = password)
    sftp = paramiko.SFTPClient.from_transport(sf)
    try:
        if os.path.isdir(local):#判斷本地參數是目錄仍是文件
            for f in os.listdir(local):#遍歷本地目錄
                sftp.put(os.path.join(local+f),os.path.join(remote+f))#上傳目錄中的文件
        else:
            sftp.put(local,remote)#上傳文件
    except Exception,e:
        print('upload exception:',e)
    sf.close()

def sftp_download(host,port,username,password,local,remote):
    sf = paramiko.Transport((host,port))
    sf.connect(username = username,password = password)
    sftp = paramiko.SFTPClient.from_transport(sf)
    try:
        if os.path.isdir(local):#判斷本地參數是目錄仍是文件
            for f in sftp.listdir(remote):#遍歷遠程目錄
                 sftp.get(os.path.join(remote+f),os.path.join(local+f))#下載目錄中文件
        else:
            sftp.get(remote,local)#下載文件
    except Exception,e:
        print('download exception:',e)
    sf.close()

if __name__ == '__main__':
    host = '111.111.1111.2'#主機
    port = 22 #端口
    username = 'root' #用戶名
    password = '123456' #密碼
    local = '/sftptest/'#本地文件或目錄,與遠程一致,若當前爲windows目錄格式,window目錄中間須要使用雙斜線
    remote = '/opt/test/'#遠程文件或目錄,與本地一致,當前爲linux目錄格式
    sftp_upload(host,port,username,password,local,remote)#上傳
    #sftp_download(host,port,username,password,local,remote)#下載
複製代碼

總結: 在python中這兩種協議實現文件的上傳與下載須要引入不一樣的模塊,實現起來仍是比較簡單的,相關模塊裏的源碼也是比較清晰。由於我須要的是天天定時下載文件,因此是在linux配置的定時天天早晨6點執行該python腳本的任務,因此文件名都是用日期命名的。 TP.quit()與FTP.close()的區別 FTP.quit():發送QUIT命令給服務器並關閉掉鏈接。這是一個比較「緩和」的關閉鏈接方式,可是若是服務器對QUIT命令返回錯誤時,會拋出異常。 FTP.close():單方面的關閉掉鏈接,不該該用在已經關閉的鏈接以後,例如不該用在FTP.quit()以後。服務器

相關文章
相關標籤/搜索