本文整理汇总了Python中urllib.request.Request.headers['Range']方法的典型用法代码示例。如果您正苦于以下问题:Python Request.headers['Range']方法的具体用法?Python Request.headers['Range']怎么用?Python Request.headers['Range']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request.Request
的用法示例。
在下文中一共展示了Request.headers['Range']方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: a
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import headers['Range'] [as 别名]
def a(url):
file = url.split('/')[-1]
u = urlopen(url)
meta = u.info()
file_size = int(meta.get_all("Content-Length")[0])
file_dl = 0
block_sz = 8192
if os.path.exists(file) and file_size == os.path.getsize(file):
print("The file '%s' already exist." % file)
exit()
elif os.path.exists(file) and file_size != os.path.getsize(file):
print("Resuming Download")
f = open(file, "ab")
dld = os.path.getsize(file)
print("Downloading: {} Bytes: {}".format(file, file_size))
while True:
buffer = u.read(dld)
if not buffer:
break
req = Request(url)
req.headers['Range'] = 'bytes=%s-%s' % (dld, file_size)
buffer = urlopen(req).read()
file_dl += len(buffer)
f.write(buffer)
remain = dld * 100./ file_size
status = "\r%10d [%3.2f%%]" % (file_dl, file_dl * remain / file_size)
status = status + chr(8)*(len(status)+1)
time.sleep(1)
sys.stdout.write(status)
sys.stdout.flush()
f.close()
print("File: %s Downloaded Successfully" % (file))
exit()
f = open(file, 'wb')
print("Downloading: {} Bytes: {}".format(file, file_size))
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_dl += len(buffer)
f.write(buffer)
status = "\r%10d [%3.2f%%]" % (file_dl, file_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
time.sleep(1)
sys.stdout.write(status)
sys.stdout.flush()
f.close()
print("File: %s Downloaded Successfully" % (file))