python下载文件损坏

编程入门 行业动态 更新时间:2024-10-26 11:16:45

python下载<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件损坏"/>

python下载文件损坏

这是我的第一篇文章。我一直在进行python编程,最近正在开发一个多线程下载器。但问题是我的文件(jpg是我的目标)被损坏。另外随着followinf输入:.jpgPython简单的多线程下载文件损坏

它显示错误

的文件被损坏。

下面是代码: -

import os, sys, requests

import threading

import urllib2

import time

URL = sys.argv[1]

def buildRange(value, numsplits):

lst = []

for i in range(numsplits):

if i == 0:

lst.append('%s-%s' % (i, int(round(1 + i * value/(numsplits*1.0) + value/(numsplits*1.0)-1, 0))))

else:

lst.append('%s-%s' % (int(round(1 + i * value/(numsplits*1.0),0)), int(round(1 + i * value/(numsplits*1.0) + value/(numsplits*1.0)-1, 0))))

return lst

def main(url=None, splitBy=5):

start_time = time.time()

if not url:

print "Please Enter some url to begin download."

return

fileName = "image.jpg"

sizeInBytes = requests.head(url, headers={'Accept-Encoding': 'identity'}).headers.get('content-length', None)

print "%s bytes to download." % sizeInBytes

if not sizeInBytes:

print "Size cannot be determined."

return

dataDict = {}

# split total num bytes into ranges

ranges = buildRange(int(sizeInBytes), splitBy)

def downloadChunk(idx, irange):

req = urllib2.Request(url)

req.headers['Range'] = 'bytes={}'.format(irange)

dataDict[idx] = urllib2.urlopen(req).read()

# create one downloading thread per chunk

downloaders = [

threading.Thread(

target=downloadChunk,

args=(idx, irange),

)

for idx,irange in enumerate(ranges)

]

# start threads, let run in parallel, wait for all to finish

for th in downloaders:

th.start()

for th in downloaders:

th.join()

print 'done: got {} chunks, total {} bytes'.format(

len(dataDict), sum((

len(chunk) for chunk in dataDict.values()

))

)

print "--- %s seconds ---" % str(time.time() - start_time)

if os.path.exists(fileName):

os.remove(fileName)

# reassemble file in correct order

with open(fileName, 'w') as fh:

for _idx,chunk in sorted(dataDict.iteritems()):

fh.write(chunk)

print "Finished Writing file %s" % fileName

print 'file size {} bytes'.format(os.path.getsize(fileName))

if __name__ == '__main__':

main(URL)

这里的缺口可能是错了,所以这里是代码引擎收录(点)的COM/wGEkp878

我会很感激,如果有人能指出错误

编辑:由人提议

def buildRange(value, numsplits):

lst = []

for i in range(numsplits):

first = i if i == 0 else buildRange().start(i, value, numsplits)

second = buildRange().end(i, value, numsplits)

lst.append("{}-{}".format(first, second))

return lst

谁能告诉我锄保持part1 part2等名称下载的零件文件等

2015-09-14

AKM

+0

作为第一个猜它看起来你有更为复杂,它的buildRange功能应该是,这也可能是你的问题。更重要的是,我很抱歉,这不是你的问题的答案,但多线程下载这样的下载几乎肯定会花费更多的时间,而不是在单个请求中进行。原因是尽管您的所有数据都在同一时间下载,但您仍然受限于带宽,现在您还有许多其他事情正在进行。虽然这是一个很酷的实验,但绝对值得完成。 –

+0

你能告诉我如何存储作为part 1 2 3 4 etc下载的零件文件吗? –

+0

你原来的构建范围似乎工作,但新的做得好得多。真正的问题似乎是额外的新行字符被添加!每遇到'\ n',在它之前插入一个额外的0x0D。 –

更多推荐

python下载文件损坏

本文发布于:2024-03-10 01:22:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1726658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!