Python简单的多线程下载程序文件已损坏

时间:2015-09-14 04:26:31

标签: python multithreading

这是我的第一篇文章。我已经做了很长时间的python编程,最近正在研究一个多线程下载器。但问题是我的文件(jpg是我的目标)被破坏了。还有以下输入:http://www.aumathletics.com/images_web/headerAUMLogo.jpg

显示错误

输入时

http://www.nasa.gov/images/content/607800main_kepler1200_1600-1200.jpg

文件已损坏。

以下是代码: -

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)

这里的缩进可能有误,所以这里是代码pastebin(dot)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等等

0 个答案:

没有答案