forked from gxcuizy/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload_folder.py
61 lines (50 loc) · 1.88 KB
/
download_folder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
通过paramiko从远处服务器下载文件资源到本地
author: gxcuizy
time: 2018-08-01
"""
import paramiko
import os
from stat import S_ISDIR as isdir
def down_from_remote(sftp_obj, remote_dir_name, local_dir_name):
"""远程下载文件"""
remote_file = sftp_obj.stat(remote_dir_name)
if isdir(remote_file.st_mode):
# 文件夹,不能直接下载,需要继续循环
check_local_dir(local_dir_name)
print('开始下载文件夹:' + remote_dir_name)
for remote_file_name in sftp.listdir(remote_dir_name):
sub_remote = os.path.join(remote_dir_name, remote_file_name)
sub_remote = sub_remote.replace('\\', '/')
sub_local = os.path.join(local_dir_name, remote_file_name)
sub_local = sub_local.replace('\\', '/')
down_from_remote(sftp_obj, sub_remote, sub_local)
else:
# 文件,直接下载
print('开始下载文件:' + remote_dir_name)
sftp.get(remote_dir_name, local_dir_name)
def check_local_dir(local_dir_name):
"""本地文件夹是否存在,不存在则创建"""
if not os.path.exists(local_dir_name):
os.makedirs(local_dir_name)
if __name__ == "__main__":
"""程序主入口"""
# 服务器连接信息
host_name = '172.17.2.18'
user_name = 'dev'
password = 'dev@zdlh'
port = 22
# 远程文件路径(需要绝对路径)
remote_dir = '/data/nfs/zdlh/pdf/2018/07/31'
# 本地文件存放路径(绝对路径或者相对路径都可以)
local_dir = 'file_download/'
# 连接远程服务器
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
# 远程文件开始下载
down_from_remote(sftp, remote_dir, local_dir)
# 关闭连接
t.close()