本文整理汇总了Python中urllib.request.Request.headers["Range"]方法的典型用法代码示例。如果您正苦于以下问题:Python Request.headers["Range"]方法的具体用法?Python Request.headers["Range"]怎么用?Python Request.headers["Range"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request.Request
的用法示例。
在下文中一共展示了Request.headers["Range"]方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: startDownload
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import headers["Range"] [as 别名]
def startDownload(self, url, start=None):
self.isPaused = False
self.url = url
self.fileName = url.split("/")[-1]
if start is not None: #if you are resuming
req = Request(url) #create a request
req.headers["Range"] = "bytes=%s-%s" %(start, self.fileSize) #set where you want to start downloading
self.u = urlopen(req) #create the connection
else:
self.u = urlopen(url) #create the connection
f = open(self.fileName, "ab") #create or append the file
self.fileSize = int(self.u.getheader("Content-Length")) #set the file size
self.szDownloaded = 0
if start is not None: #if resuming
self.szDownloaded = start #downloaded = where you started
block_sz = 8192 #how many bytes do you want to download per interaction
while True: #download block_sz bytes per interaction
buffer = self.u.read(block_sz) #download the bytes
if not buffer: #if there is nothing, download ok
self.status = 1 # value 1 = "Download complete"
break
self.szDownloaded += len(buffer) #append the downloaded
f.write(buffer) #write bytes on file
self.status = 0 #value 0 = "Downloading
print(self.szDownloaded) #show how many bytes were downloaded
if self.isPaused is True: #if is pause, go away
break
f.close()
示例2: download_cdn_videos
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import headers["Range"] [as 别名]
def download_cdn_videos(filenames,sub_urls,handout_urls,video_urls, target_dir):
""" This function downloads the videos from video_urls """
""" using a simple file downloader """
for i, v in enumerate(video_urls):
filename_prefix = str(i+1).zfill(2) + '-'
#original_filename = v.rsplit('/', 1)[1]
video_filename = filename_prefix + filenames[i] + '.mp4'
sub_filename = filename_prefix + filenames[i] + '.srt'
handout_filename = filename_prefix + filenames[i] + '.srt'
video_path = os.path.join(target_dir, video_filename)
sub_path = os.path.join(target_dir, sub_filename)
handout_path = os.path.join(target_dir, handout_filename)
#print('[debug] GET %s' % v)
print('[download] Destination: %s' % video_path)
v = quote(v,safe=":/")
if len(v) != YOUTUBE_VIDEO_ID_LENGTH:
req = Request(v)
try:
video = urlopen(v)
fileSize = int(video.headers['content-length'])
finish = False
existSize = 0
if os.path.exists(video_path):
output = open(video_path,"ab")
existSize = os.path.getsize(video_path)
#If the file exists, then only download the remainder
if existSize < fileSize:
#print("[debug] bytes range is: %s-%s" % (existSize,fileSize))
req.headers["Range"]= "bytes=%s-%s" % (existSize,fileSize)
video = urlopen(req)
else:
finish = True
else:
output = open(video_path,"wb")
if finish == False:
file_size_dl = existSize
block_sz = 262144
while True:
buffer = video.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
output.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / fileSize)
status = status + chr(8)*(len(status)+1)
sys.stdout.write(status)
sys.stdout.flush()
output.close()
except URLError as e:
print("[warning]error: %r when downloading %s" % (e.reason,v) )
else:
download_youtube_video(v,video_path)
if sub_urls[i] != "":
#print('[debug] GET %s' % BASE_URL+sub_urls[i])
if not os.path.exists(sub_path):
subs_string = edx_get_subtitle(sub_urls[i], headers)
if subs_string:
print('[info] Writing edX subtitles: %s' % sub_path)
open(os.path.join(os.getcwd(), sub_path),
'wb+').write(subs_string.encode('utf-8'))
if handout_urls[i] != "":
#print('[debug] GET %s' % BASE_URL+sub_urls[i])
if not os.path.exists(handout_path):
handout_content = urlopen(BASE_URL+handout_urls[i]).read()
if handout_content:
print('[info] Writing handout: %s' % handout_path)
open(os.path.join(os.getcwd(), handout_path),
'wb+').write(handout_content)