# 在python3下測試import sysimport requestsimport threadingimport datetimeimport time# 傳入的命令行參數,要下載文件的urlurl = ""def Handler(start, end, url, filename): headers = {'Range': 'bytes=%d-%d' % (start, end)} r = requests.get(url, headers=headers, stream=True) # 寫入文件對應位置 with open(filename, "r+b") as fp: fp.seek(start) var = fp.tell() fp.write(r.content) fp.close() print(start,end)def download_file(url, num_thread=5): r = requests.head(url) try: file_name = url.split('/')[-1] file_size = int( r.headers['content-length']) # Content-Length得到文件主體的大小,當http服務器使用Connection:keep-alive時,不支持Content-Length except: print("檢查URL,或不支持對線程下載") return # 建立一個和要下載文件同樣大小的文件 print("create file") fp = open("D:\\HOTA\\UpdateBasePackage.zip", "w") fp.truncate(file_size) fp.close() time.sleep(5) # 啓動多線程寫文件 num_thread = 100 part = file_size // num_thread # 若是不能整除,最後一塊應該多幾個字節 # num_thread = 10 # part = 100*1024*1024 tempList=[] for i in range(num_thread): start = part * i if i == num_thread - 1: # 最後一塊 end = file_size else: end = start + part temp = [start,end] tempList.append(temp) print(tempList) tag = 0 runningThread = [] while True: if len(runningThread)<5: t = threading.Thread(target=Handler, kwargs={'start': tempList[tag][0], 'end': tempList[tag][1], 'url': url, 'filename': "D:\\HOTA\\UpdateBasePackage.zip"}) # t.setDaemon(True) t.start() runningThread.append(t) tag = tag + 1 if tag == 100: break for t in runningThread: if not t.isAlive(): runningThread.remove(t) t.join() print(runningThread) time.sleep(1) # 等待全部線程下載完成 main_thread = threading.current_thread() for t in threading.enumerate(): if t is main_thread: continue t.join() print('%s 下載完成' % file_name)if __name__ == '__main__': start = datetime.datetime.now().replace(microsecond=0) download_file(url) end = datetime.datetime.now().replace(microsecond=0) print("用時: ", end='') print(end - start)