Last active
October 16, 2018 11:17
-
-
Save xl7dev/bf51d67f68a07629aa3c75385c90e050 to your computer and use it in GitHub Desktop.
python request文件下载续传
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
@author: xl7dev | |
""" | |
import sys | |
import os | |
import cgi | |
import re | |
import datetime | |
import requests | |
class DOWNLOAD(): | |
def getOffset(self, local_path): | |
""" | |
获取已下载文件大小 | |
:param local_path: | |
:return: | |
""" | |
if os.path.exists(local_path): | |
return os.path.getsize(local_path) | |
else: | |
return 0 | |
def dl(self, url): | |
""" | |
下载文件 | |
:param url: | |
:return: | |
""" | |
# 获取文件属性 | |
r = requests.head(url) | |
start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
filesize = int(r.headers['Content-Length']) # 获取下载文件大小 | |
if 'Content-disposition' in r.headers: | |
value, params = cgi.parse_header(r.headers['Content-disposition']) | |
filename = params['filename'] # 获取下载文件名 | |
else: | |
filename = url.split('/')[-1] | |
# 检查断点续传 | |
start = self.getOffset(filename) | |
if start != filesize: | |
end = int(filesize) - 1 | |
range = "bytes={0}-{1}".format(start, end) | |
headers = {"Range": range} | |
# 开始下载 | |
r = requests.get(url, stream=True, headers=headers, verify=False) | |
with open(filename, 'ab+') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
print(chunk) | |
f.write(chunk) | |
end_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
item = {"filename": filename, "filesize": filesize, "start_time": start_time, | |
"end_time": end_time, "url":url} | |
print(item) | |
return item | |
else: | |
print("Download file {0} completed!".format(filename)) | |
if __name__ == "__main__": | |
url = sys.argv[1] | |
demo = DOWNLOAD() | |
demo.dl(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment