编写一个支持断点续传功能的 HTTP 容错客户端

编写一个容错的客户端,它在因某种原因初次下载失败后能继续下载文件。
从www.python.org上下载Python 3.7的源码。使用resume_download()函数下载的文件在中止后能继续下载尚未下载的内容。

import urllib 
import os

TARGET_URL = 'https://www.python.org/ftp/python/3.7.4/' 
TARGET_FILE = 'Python-3.7.4.tgz' 
class CustomURLOpener(urllib.FancyURLopener): 
	def http_error_206(self, url, fp, errcode, errmsg, headers, data=None): 
		pass 
def resume_download(): 
	file_exists = False 
	CustomURLClass = CustomURLOpener() 
	if os.path.exists(TARGET_FILE): 
		out_file = open(TARGET_FILE,"ab") 
		file_exists = os.path.getsize(TARGET_FILE) 
		#If the file exists, then only download the unfinished part 
		CustomURLClass.addheader("range","bytes=%s-" % (file_exists)) 
	else: 
		out_file = open(TARGET_FILE,"wb") 
	web_page = CustomURLClass.open(TARGET_URL + TARGET_FILE) 
	#Check if last download was OK 
	if int(web_page.headers['Content-Length']) == file_exists: 
		loop = 0 
		print("File already downloaded!")
	byte_count = 0 
	while True: 
		data = web_page.read(8192) 
		if not data: 
			break 
		out_file.write(data) 
		byte_count = byte_count + len(data) 
	web_page.close()
	out_file.close() 
	for k,v in web_page.headers.items(): 
		print(k, "=",v)
		print("File copied", byte_count, "bytes from", web_page.url)
if __name__ == '__main__': 
	resume_download()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值