Python脚本下载ftp的指定数据

 1 # _*_ coding:utf-8 _*_
 2 
 3 """
 4     __title__ = 'download ftp xml data'
 5     __author__ = 'Lucky'
 6     __date__ = '2018/07/06
 7 """
 8 
 9 import ftplib
10 import time
11 
12 class Ftp_download:
13 
14     def __init__(self,host,username,password,str_time,file,port=21,bufsize=1024):
15         self.ftp = ftplib.FTP()
16         self.host = host
17         self.username = username
18         self.password = password
19         self.str_time = str_time
20         self.file = file
21         self.port = port
22         self.bufsize = bufsize
23 
24     def ftp_download(self):
25         self.ftp.connect(self.host, self.port,timeout=20)
26         self.ftp.login(self.username,self.password)
27 
28         file_local_dir = '/home/tmp/test/' + self.file
29         data_file = self.ftp.nlst()
30 
31         count = 0
32         while count < 4:
33             if self.file in data_file:
34                 fp = open(file_local_dir, 'wb')
35                 self.ftp.retrbinary('RETR %s' % self.file, fp.write, self.bufsize)
36                 return True
37             else:
38                 time.sleep(500)
39                 count += 1
40                 continue
41         return False
42 
43     def logout(self):
44         self.ftp.close()
45 
46 def main():
47 
48     str_time = time.strftime("%Y%m%d", time.localtime())
49     file = str_time + ".tar" + ".gz"
50   
51     new_ftp = Ftp_download('192.168.2.3', 'testuser', 'testpasswd', str_time, file)  # 传递ftp用户名密码时间和文件名
52 
53     if new_ftp.ftp_download():
54         new_ftp.logout()
55     else:
56         new_ftp.logout()
57 
58 if __name__ == "__main__":
59     main()

 

转载于:https://www.cnblogs.com/zhangweiyi/p/10564674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值