Python_實現(xiàn)ftp上傳和下載
來源:menglei8625
發(fā)布時間:2018-11-03 16:12:27
閱讀量:1068
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不存在,繼續(xù)...' %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 發(fā)生錯誤: %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 成功執(zhí)行了備份\n" %datenow