| 328 | |
| 329 | # 下载文件 |
| 330 | def DownloadFile(self, url, filename): |
| 331 | import requests |
| 332 | self.pre = 0 |
| 333 | self.oldTime = time.time() |
| 334 | self.WriteLogs("正在下载文件【{}】...".format(filename)) |
| 335 | |
| 336 | try: |
| 337 | download_res = requests.get(url, headers=public.get_requests_headers(), timeout=30, stream=True) |
| 338 | headers_total_size = int(download_res.headers['content-length']) |
| 339 | res_chunk_size = 8192 * 2 |
| 340 | count = 0 |
| 341 | with open(filename, 'wb+') as with_res_f: |
| 342 | for download_chunk in download_res.iter_content(chunk_size=res_chunk_size): |
| 343 | if download_chunk: |
| 344 | count += 1 |
| 345 | with_res_f.write(download_chunk) |
| 346 | speed_last_size = len(download_chunk) |
| 347 | self.DownloadHook(count, speed_last_size, headers_total_size) |
| 348 | with_res_f.close() |
| 349 | except requests.exceptions.RequestException as e: |
| 350 | error_message = "ERROR:下载文件【{}】失败: {}".format(filename, str(e)) |
| 351 | self.WriteLogs(error_message) |
| 352 | |
| 353 | # 下载文件进度回调 |
| 354 | def DownloadHook(self, count, blockSize, totalSize): |