配置文件{"workDir": "F:/temp","zipDir": "F:/result","zipName": "rrrr","threads": [{"HostAddress": "10.7.5.69","Port": 22,"Username": "ftpuser","Password": "ftpuser","RemotePath": "/app/ftpuser/100_OrdMember_2019","LocalPath": "F:/temp/1"},{"HostAddress": "10.7.5.69","Port": 22,"Username": "ftpuser","Password": "ftpuser","RemotePath": "/app/ftpuser/100_OrdMember_2019","LocalPath": "F:/temp/2"},{"HostAddress": "10.7.5.69","Port": 22,"Username": "ftpuser","Password": "ftpuser","RemotePath": "/app/ftpuser/100_OrdMember_2019","LocalPath": "F:/temp/3"}]}
源码讲解这里我们做的其实是一个Python脚本 , 像是shell脚本 。程序入口在主方法 , 进入主方法后我们干了几件事:
声明日志 , 固定了日志输出的格式、日志等级和输出日志的目的地(日志文件和终端) 。输出日志到日志文件用于保存日志 , 便于日后查看 , 输出到终端是让用户及时了解程序在做什么 , 做到什么程度了 。
声明一个锁 。这个所是为了下载文件计数 , 保证下载文件时 , 只有一个线程可以操作变量finish 。
读取配置文件 。想要开始执行 , 得先获取咱们的配置文件 , 让程序知道它连接那一台服务器 , 下载哪个目录下的文件到本地哪个目录 , 等等……
检查本地工作目录 。为了减少用户配置的错误 , 避免他/她配了一个不存在的地址导致程序找不到目录而引发错误 , 所以我们要判断目录是否存在 , 是否需要创建目录 。
根据配置文件threads中的配置 , 循环创建线程 , 完成读取本线程需要的配置文件、连接ftp、下载文件的操作 。
压缩文件 。拼接文件目录 , 保证压缩文件的目录关系不变 , 调用压缩文件方法 。
美化结束动作 , 如果没有这一步 , 程序跑完就会关闭当前终端 , 导致用户无法了解程序是跑完了 , 还是中间出了问题关闭了 。输出一段文字告诉用户可以按Enter来结束程序 , 并且用input()监听用户的输入 , 达到保留终端的效果 。
查看日志文件2020-01-19 13:35:33,253 - root - INFO - 工作目录: F:/temp2020-01-19 13:35:33,254 - root - INFO - =======================配置信息 start=============================2020-01-19 13:35:33,256 - root - INFO - HostAddress 10.7.5.692020-01-19 13:35:33,257 - root - INFO - Port 222020-01-19 13:35:33,258 - root - INFO - Username ftpuser2020-01-19 13:35:33,259 - root - INFO - Password ftpuser2020-01-19 13:35:33,260 - root - INFO - RemotePath /app/ftpuser/100_OrdMember_20192020-01-19 13:35:33,260 - root - INFO - LocalPath F:/temp/12020-01-19 13:35:33,261 - root - INFO - =======================配置信息 end=============================2020-01-19 13:35:33,290 - paramiko.transport - INFO - Connected (version 2.0, client OpenSSH_5.5)2020-01-19 13:35:33,638 - paramiko.transport - INFO - Authentication (password) successful!2020-01-19 13:35:33,656 - paramiko.transport.sftp - INFO - [chan 0] Opened sftp connection (server version 3)2020-01-19 13:35:33,659 - root - INFO - =======================配置信息 start=============================2020-01-19 13:35:33,660 - root - INFO - HostAddress 10.7.5.692020-01-19 13:35:33,661 - root - INFO - Port 222020-01-19 13:35:33,661 - root - INFO - Username ftpuser2020-01-19 13:35:33,662 - root - INFO - Password ftpuser2020-01-19 13:35:33,663 - root - INFO - RemotePath /app/ftpuser/100_OrdMember_20192020-01-19 13:35:33,664 - root - INFO - LocalPath F:/temp/22020-01-19 13:35:33,665 - root - INFO - =======================配置信息 end=============================2020-01-19 13:35:33,680 - paramiko.transport - INFO - Connected (version 2.0, client OpenSSH_5.5)2020-01-19 13:35:33,987 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019/100_OrdMember_2019222 ==> F:/temp/1100_OrdMember_2019100_OrdMember_2019100_OrdMember_20192222020-01-19 13:35:33,987 - root - INFO - 已下载 [1] 个文件2020-01-19 13:35:34,011 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019/100_OrdMember_201922 ==> F:/temp/1100_OrdMember_2019100_OrdMember_2019100_OrdMember_2019222020-01-19 13:35:34,012 - root - INFO - 已下载 [2] 个文件2020-01-19 13:35:34,032 - paramiko.transport - INFO - Authentication (password) successful!2020-01-19 13:35:34,047 - paramiko.transport.sftp - INFO - [chan 0] Opened sftp connection (server version 3)2020-01-19 13:35:34,050 - root - INFO - =======================配置信息 start=============================2020-01-19 13:35:34,051 - root - INFO - HostAddress 10.7.5.692020-01-19 13:35:34,052 - root - INFO - Port 222020-01-19 13:35:34,052 - root - INFO - Username ftpuser2020-01-19 13:35:34,053 - root - INFO - Password ftpuser2020-01-19 13:35:34,054 - root - INFO - RemotePath /app/ftpuser/100_OrdMember_20192020-01-19 13:35:34,055 - root - INFO - LocalPath F:/temp/32020-01-19 13:35:34,056 - root - INFO - =======================配置信息 end=============================2020-01-19 13:35:34,060 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/tmp001.xls ==> F:/temp/1100_OrdMember_2019tmp001.xls2020-01-19 13:35:34,071 - root - INFO - 已下载 [3] 个文件2020-01-19 13:35:34,072 - paramiko.transport - INFO - Connected (version 2.0, client OpenSSH_5.5)2020-01-19 13:35:34,095 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019111 ==> F:/temp/1100_OrdMember_2019100_OrdMember_20191112020-01-19 13:35:34,096 - root - INFO - 已下载 [4] 个文件2020-01-19 13:35:34,118 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019/100_OrdMember_2019222 ==> F:/temp/2100_OrdMember_2019100_OrdMember_2019100_OrdMember_20192222020-01-19 13:35:34,118 - root - INFO - 已下载 [5] 个文件2020-01-19 13:35:34,123 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_201911 ==> F:/temp/1100_OrdMember_2019100_OrdMember_2019112020-01-19 13:35:34,124 - root - INFO - 已下载 [6] 个文件2020-01-19 13:35:34,258 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019/100_OrdMember_201922 ==> F:/temp/2100_OrdMember_2019100_OrdMember_2019100_OrdMember_2019222020-01-19 13:35:34,258 - root - INFO - 已下载 [7] 个文件2020-01-19 13:35:34,449 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/tmp001.xls ==> F:/temp/2100_OrdMember_2019tmp001.xls2020-01-19 13:35:34,450 - root - INFO - 已下载 [8] 个文件2020-01-19 13:35:34,464 - paramiko.transport - INFO - Authentication (password) successful!2020-01-19 13:35:34,479 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019111 ==> F:/temp/2100_OrdMember_2019100_OrdMember_20191112020-01-19 13:35:34,480 - root - INFO - 已下载 [9] 个文件2020-01-19 13:35:34,480 - paramiko.transport.sftp - INFO - [chan 0] Opened sftp connection (server version 3)2020-01-19 13:35:34,508 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_201911 ==> F:/temp/2100_OrdMember_2019100_OrdMember_2019112020-01-19 13:35:34,509 - root - INFO - 已下载 [10] 个文件2020-01-19 13:35:34,552 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019/100_OrdMember_2019222 ==> F:/temp/3100_OrdMember_2019100_OrdMember_2019100_OrdMember_20192222020-01-19 13:35:34,552 - root - INFO - 已下载 [11] 个文件2020-01-19 13:35:34,576 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019/100_OrdMember_201922 ==> F:/temp/3100_OrdMember_2019100_OrdMember_2019100_OrdMember_2019222020-01-19 13:35:34,576 - root - INFO - 已下载 [12] 个文件2020-01-19 13:35:34,624 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/tmp001.xls ==> F:/temp/3100_OrdMember_2019tmp001.xls2020-01-19 13:35:34,624 - root - INFO - 已下载 [13] 个文件2020-01-19 13:35:34,649 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_2019111 ==> F:/temp/3100_OrdMember_2019100_OrdMember_20191112020-01-19 13:35:34,649 - root - INFO - 已下载 [14] 个文件2020-01-19 13:35:34,675 - root - INFO - [get] /app/ftpuser/100_OrdMember_2019/100_OrdMember_201911 ==> F:/temp/3100_OrdMember_2019100_OrdMember_2019112020-01-19 13:35:34,675 - root - INFO - 已下载 [15] 个文件2020-01-19 13:35:34,677 - root - INFO - =======================准备开始压缩==========================2020-01-19 13:35:34,678 - root - INFO - ==========================开始压缩 。。。==========================2020-01-19 13:35:34,682 - root - INFO - 写入 F:/temp1 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,684 - root - INFO - 写入 F:/temp1100_OrdMember_2019 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,686 - root - INFO - 写入 F:/temp1100_OrdMember_2019100_OrdMember_2019 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,696 - root - INFO - 写入 F:/temp1100_OrdMember_2019100_OrdMember_2019100_OrdMember_201922 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,698 - root - INFO - 写入 F:/temp1100_OrdMember_2019100_OrdMember_2019100_OrdMember_2019222 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,700 - root - INFO - 写入 F:/temp1100_OrdMember_2019100_OrdMember_201911 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,702 - root - INFO - 写入 F:/temp1100_OrdMember_2019100_OrdMember_2019111 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,716 - root - INFO - 写入 F:/temp1100_OrdMember_2019tmp001.xls 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,717 - root - INFO - 写入 F:/temp2 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,718 - root - INFO - 写入 F:/temp2100_OrdMember_2019 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,720 - root - INFO - 写入 F:/temp2100_OrdMember_2019100_OrdMember_2019 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,723 - root - INFO - 写入 F:/temp2100_OrdMember_2019100_OrdMember_2019100_OrdMember_201922 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,738 - root - INFO - 写入 F:/temp2100_OrdMember_2019100_OrdMember_2019100_OrdMember_2019222 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,740 - root - INFO - 写入 F:/temp2100_OrdMember_2019100_OrdMember_201911 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,742 - root - INFO - 写入 F:/temp2100_OrdMember_2019100_OrdMember_2019111 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,745 - root - INFO - 写入 F:/temp2100_OrdMember_2019tmp001.xls 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,768 - root - INFO - 写入 F:/temp3 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,770 - root - INFO - 写入 F:/temp3100_OrdMember_2019 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,772 - root - INFO - 写入 F:/temp3100_OrdMember_2019100_OrdMember_2019 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,774 - root - INFO - 写入 F:/temp3100_OrdMember_2019100_OrdMember_2019100_OrdMember_201922 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,776 - root - INFO - 写入 F:/temp3100_OrdMember_2019100_OrdMember_2019100_OrdMember_2019222 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,790 - root - INFO - 写入 F:/temp3100_OrdMember_2019100_OrdMember_201911 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,792 - root - INFO - 写入 F:/temp3100_OrdMember_2019100_OrdMember_2019111 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,794 - root - INFO - 写入 F:/temp3100_OrdMember_2019tmp001.xls 到压缩包 F:/resultrrrr.zip 成功2020-01-19 13:35:34,794 - root - INFO - ==========================压缩完成==========================
推荐阅读
- 白茶可以治疗糖尿病,喝白茶有控制血糖的作用
- 不可忽视的茶暗语,茶药膳疗法对保健起着不可忽视的作用
- 绿茶的作用,绿茶作用,绿茶的防癌与抗癌的效果作用
- 白毫银针用多少度水泡,白毫银针十不采
- 藏红花茶的功效作用,苦荞茶的功效与作用
- 2020最受欢迎的python编辑器和IDE
- 很多公司出现这个问题,有线和无线两个局域网,如何同时用打印机
- 与黄河有关的诗词名句及作者?三句描写黄河的诗句并注明作者和出处意思
- 《竹》苏轼?苏轼画的竹子
- h3c常用巡检命令?h3c巡检命令