python 從遠程批量下載文件到本地

需求:

1、從postgresql數據庫中查出附件名稱

2、從遠程服務器下載對應的附件

用到的python模塊paramiko、psycopg2。

paramiko是用python寫的一個模塊,遵循SSH2協議,支持以加密和認證的方式,進行遠程服務器的連接。利用該模塊,可以方便的進行ssh連接和sftp協議進行sftp文件傳輸以及遠程命令執行。
psycopg2是python的postgresql數據庫接口,可以對數據庫進行操作。

conndb.py文件代碼功能是連接數據庫,查詢附件的名稱,用於拼接地址。
import psycopg2

def conn_db():
    # database,user,password,host,port分別對應要連接的PostgreSQL數據庫的數據庫名、數據庫用戶名、用戶密碼、主機、端口信息
    conn = psycopg2.connect(database="h", user="oe", password="1234", host="10.18.xxx.xxx", port="5432")
    cursor = conn.cursor()
    # 執行查詢命令
    cursor.execute("select store_fname,datas_fname from contract_attachment where contract_interview_id in(select id from hr_re)")

    result = cursor.fetchall()
    print(result)
    # cursor.close()   #關閉遊標
    conn.close()     #關閉連接
    return result
download.py進行連接服務器和下載文件的操作
from conndb import conn_db
import os
import re
import logging
import paramiko
import base64

_logger = logging.getLogger(__name__)
PATH = '/hr/openerp8/openerp/'
LOCATION='file:///filestore'
dbname='h'

def full_path(path):
    # location = 'file:filestore'
    assert LOCATION.startswith('file:'), "Unhandled filestore location %s" % LOCATION
    location = LOCATION[5:]

    # sanitize location name and path
    location = re.sub('[.]', '', location)
    location = location.strip('/\\')

    path = re.sub('[.]', '', path)
    path = path.strip('/\\')
    res_path =os.path.join(PATH, location, dbname, path)
    return  res_path.replace('\\','/')


def data_get(data):
    result = []

    bin_size = False
    for attach in data:
        # f_path = None
        if LOCATION and attach[0]:
            f_path = full_path(attach[0])
            result.append(f_path)
            # os.remove(f_path)

    return result



def RemoteScp(host_ip, host_port, host_username, host_password, remote_file, local_file):
    scp = paramiko.Transport((host_ip, host_port))
    scp.connect(username=host_username, password=host_password)
    sftp = paramiko.SFTPClient.from_transport(scp)
    for file in remote_file:
        file_path,filename =os.path.split(file)
        sftp.get(file, local_file+'/'+filename)
    scp.close()
    return ("success")

if __name__ == '__main__':
    ip="10.18.xxx.xxx"
    user="root"
    passwd="1234"
    host_port = 22

    data = conn_db() #取文件名集合
    all_file = data_get(data)
    local_path = 'F:/ProjectTrain/download_file/att'

    RemoteScp(ip, host_port, user, passwd, all_file, local_path)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章