Python之FTP多线程下载文件之分块多线程文件合并

时间:2023-03-09 16:44:17
Python之FTP多线程下载文件之分块多线程文件合并

Python之FTP多线程下载文件之分块多线程文件合并

欢迎大家阅读Python之FTP多线程下载系列之二:Python之FTP多线程下载文件之分块多线程文件合并,本系列的第一篇:Python之FTP多线程下载文件之多线程分块下载文件,主要讲述了Python中如何使用多线程对文件进行分块下载。

今天,我们接着上篇的思路,我们利用多线程对文件进行下载,待全部文件下载完成之后,我们需要对各个文件块进行合并,合并的过程比较简单:

Python之FTP多线程下载文件之分块多线程文件合并
 1 def mergerFile(self, localFile, threadNumber):
2 """
3 Meger all the sub parts of the file into 1 file
4 another thread will be call to do this
5 """
6 try:
7 while 1:
8 subThread = threading.Thread(target = self.mergeFileExecutor, args = (localFile, threadNumber,))
9 subThread.start()
10 subThread.join()
11 if 1 == self.mergerFlag:
12 self.mergerFlag = 0
13 return False
14 # check if total size of part file equals to size of the whole file
15 localFileSize = os.path.getsize(localFile)
16 totalSize = 0
17 for i in range(0, threadNumber):
18 totalSize += os.path.getsize(localFile + '.part.' + str(i))
19 if localFileSize == totalSize:
20 break
21 return True
22 except Exception, diag:
23 self.recordLog(str(diag), 'error')
24 return False
Python之FTP多线程下载文件之分块多线程文件合并

其中的mergeFileExecutor函数如下:

Python之FTP多线程下载文件之分块多线程文件合并
 1 def mergerFileExecutor(self, localFile, threadNumber):
2 try:
3 errorFlag = 0
4 fw = open(localFile, 'wb')
5 for i in range(0, threadNumber):
6 fname = localFile + '.part.' + str(i)
7 if not os.path.exists(fname):
8 errorFlag = 1
9 break
10 fr = open(fname, 'rb')
11 data = fr.read()
12 time.sleep(2)
13 fr.close()
14 fw.write(data)
15 fw.flush()
16 time.sleep(1)
17 fw.close()
18 if 1 == errorFlag:
19 # some part file is not available
20 self.mergerFlag = 1
21 except Exception, diag:
22 # error occr
23 self.mergerFlag = 1
24 self.recordLog(str(diag), 'error')
Python之FTP多线程下载文件之分块多线程文件合并

好了,我们分块下载到的文件已经被成功合并为一整个文件,当然为了保证文件的完整性,我们可以采用诸如md5sum等方式对文件进行验证,在此我采用了比较简单的比较文件大小的方式来验证文件的完整性,验证代码如下:

Python之FTP多线程下载文件之分块多线程文件合并
 1 def checkSizeEqual(self, remoteFile, localFile):
2 '''
3 check the remote file size and the local file size.
4 if =, return true,
5 else, return false
6 '''
7 try:
8 remoteFileSize = self.ftp.size(remoteFile)
9 localFileSize = os.path.getsize(localFile)
10 if localFileSize == remoteFileSize:
11 return True
12 else:
13 return False
14 except Exception, diag:
15 print diag
Python之FTP多线程下载文件之分块多线程文件合并

好了,完成上述的准备工作,我们就可以进行FTP多线程文件下载的操作了,开始我们的main函数之旅吧!今天的内容就这些,在下一节,我将会给出main函数代码,并给出完整的FTP类的全部源代码下载,敬请关注!

感谢大家的阅读,希望能够帮到大家!