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)

 

你可能感兴趣的:(python)