python 从远程批量下载文件到本地

系统 2421 0

需求:

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)


            
          

 


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论