python requests 断点续传下载

来源:互联网 发布:李昌钰有多厉害知乎 编辑:程序博客网 时间:2024/06/05 06:28

1.安装requests:

pip install requests

2.实例:

# # encoding:utf-8import requestsimport osimport timedef get_local_file_exists_size(local_path):    try:        lsize = os.stat(local_path).st_size    except:        lsize = 0    return lsizedef get_file_obj(down_link, offset):    webPage = None    try:        headers = {'Range': 'bytes=%d-' % offset}        webPage = requests.get(down_link, stream=True, headers=headers, timeout=120, verify=False)        status_code = webPage.status_code        if status_code in [200, 206]:            webPage = webPage        elif status_code == 416:            print u"%s文件数据请求区间错误,status_code:%s" % (down_link, status_code)        else:            print u"%s链接有误,status_code:%s" % (down_link, status_code)    except Exception as e:        print u"无法链接:%s,e:%s" % (down_link, e)    finally:        return webPagedown_link = '' #下载链接file_size = 271768736 #文件总大小local_path = "/home/adger/image/test.mp4"while True:    lsize = get_local_file_exists_size(local_path)    if lsize == file_size:        break    webPage = get_file_obj(down_link, lsize)    try:        file_obj = open(local_path, 'ab+')    except Exception as e:        print u"打开文件:%s失败" % local_path        break    try:        for chunk in webPage.iter_content(chunk_size=64 * 1024):            if chunk:                file_obj.write(chunk)            else:                break    except Exception as e:        time.sleep(5)    file_obj.close()    webPage.close()


1 0
原创粉丝点击