Python_实现ftp上传和下载
来源:menglei8625
发布时间:2018-11-03 16:12:27
阅读量:1088
4. ftp自动下载、自动上传脚本,可以递归目录操作
7. from ftplib import FTP
8. import os,sys,string,datetime,time
12. def __init__(self, hostaddr, username, password, remotedir, port=21):
13. self.hostaddr = hostaddr
14. self.username = username
15. self.password = password
16. self.remotedir = remotedir
28. socket.setdefaulttimeout(timeout)
30. print '开始连接到 %s' %(self.hostaddr)
31. ftp.connect(self.hostaddr, self.port)
32. print '成功连接到 %s' %(self.hostaddr)
33. print '开始登录到 %s' %(self.hostaddr)
34. ftp.login(self.username, self.password)
35. print '成功登录到 %s' %(self.hostaddr)
36. debug_print(ftp.getwelcome())
38. deal_error("连接或登录失败")
40. ftp.cwd(self.remotedir)
44. def is_same_size(self, localfile, remotefile):
46. remotefile_size = self.ftp.size(remotefile)
50. localfile_size = os.path.getsize(localfile)
53. debug_print('lo:%d re:%d' %(localfile_size, remotefile_size),)
54. if remotefile_size == localfile_size:
58. def download_file(self, localfile, remotefile):
59. if self.is_same_size(localfile, remotefile):
60. debug_print('%s 文件大小相同,无需下载' %localfile)
63. debug_print('>>>>>>>>>>>>下载文件 %s ... ...' %localfile)
65. file_handler = open(localfile, 'wb')
66. self.ftp.retrbinary('RETR %s'%(remotefile), file_handler.write)
69. def download_files(self, localdir='./', remotedir='./'):
71. self.ftp.cwd(remotedir)
73. debug_print('目录%s不存在,继续...' %remotedir)
75. if not os.path.isdir(localdir):
76. os.makedirs(localdir)
77. debug_print('切换至目录 %s' %self.ftp.pwd())
79. self.ftp.dir(self.get_file_list)
80. remotenames = self.file_list
83. for item in remotenames:
86. local = os.path.join(localdir, filename)
88. self.download_files(local, filename)
89. elif filetype == '-':
90. self.download_file(local, filename)
92. debug_print('返回上层目录 %s' %self.ftp.pwd())
93. def upload_file(self, localfile, remotefile):
94. if not os.path.isfile(localfile):
96. if self.is_same_size(localfile, remotefile):
97. debug_print('跳过[相等]: %s' %localfile)
99. file_handler = open(localfile, 'rb')
100. self.ftp.storbinary('STOR %s' %remotefile, file_handler)
101. file_handler.close()
102. debug_print('已传送: %s' %localfile)
103. def upload_files(self, localdir='./', remotedir = './'):
104. if not os.path.isdir(localdir):
106. localnames = os.listdir(localdir)
107. self.ftp.cwd(remotedir)
108. for item in localnames:
109. src = os.path.join(localdir, item)
110. if os.path.isdir(src):
114. debug_print('目录已存在 %s' %item)
115. self.upload_files(src, item)
117. self.upload_file(src, item)
120. def get_file_list(self, line):
122. file_arr = self.get_filename(line)
123. if file_arr[1] not in ['.', '..']:
124. self.file_list.append(file_arr)
126. def get_filename(self, line):
127. pos = line.rfind(':')
128. while(line[pos] != ' '):
130. while(line[pos] == ' '):
132. file_arr = [line[0], line[pos:]]
137. timenow = time.localtime()
138. datenow = time.strftime('%Y-%m-%d', timenow)
139. logstr = '%s 发生错误: %s' %(datenow, e)
144. if __name__ == '__main__':
145. file = open("log.txt", "a")
146. timenow = time.localtime()
147. datenow = time.strftime('%Y-%m-%d', timenow)
150. hostaddr = 'localhost'
154. rootdir_local = '.' + os.sep + 'bak/'
155. rootdir_remote = './'
157. f = MYFTP(hostaddr, username, password, rootdir_remote, port)
159. f.download_files(rootdir_local, rootdir_remote)
161. timenow = time.localtime()
162. datenow = time.strftime('%Y-%m-%d', timenow)
163. logstr += " - %s 成功执行了备份\n" %datenow