Python多线程下载(2)
上篇:http://www.zzzyk.com/kf/201112/113937.html
#encoding: gbk
from urllib.request import *
from threading import *
import time
import os
PIECE_SIZE = 1024
class Block:
def __init__(self, fileInfo, startPiece, endPiece, last = False, lastSize = None):
self.fileInfo = fileInfo
self.startPiece = startPiece
self.endPiece = endPiece
self.last = last
self.lastSize = lastSize
# [startPos, endPos]闭区间
self.startPos = PIECE_SIZE * startPiece
if not self.last:
self.endPos = PIECE_SIZE * endPiece - 1
else:
self.endPos = PIECE_SIZE * endPiece - (PIECE_SIZE - lastSize) - 1
print("range:(%d-%d)"%(self.startPos, self.endPos))
def getNext(self):
s = -1
e = self.endPiece
#print(self.fileInfo.pieceMap[self.startPiece:self.endPiece])
for i in range(self.startPiece, self.endPiece):
if s == -1 and self.fileInfo.pieceMap[i] == ord('0'):
s = i
if s >= 0 and self.fileInfo.pieceMap[i] == ord('1'):
e = i
break
print("getNext.raw(", s, e, ")")
if s >= 0 and e > s:
start = PIECE_SIZE * s
end = PIECE_SIZE * e
if end > self.endPos:
end = self.endPos
self.pos = start
print("getNext.result(%d, %d)"%(start, end))
return (start, end)
else:
return None
def write(self, data):
self.fileInfo.write(self.pos, data)
with self.fileInfo.lock:
s = self.pos
self.pos += len(data)
e = self.pos
if self.last and self.pos >= self.endPos:
self.fileInfo.pieceMap[self.endPiece - 1] = ord('1')
#print("set(%d)"%(self.endPiece - 1))
sp = s // PIECE_SIZE
ep = e // PIECE_SIZE
for i in range(sp, ep):
self.fileInfo.pieceMap[i] = ord('1')
#print("set(%d)"%i)
#self.fileInfo.saveInfo()
class FileInfo:
def __init__(self, fileName, fileSize, numBlocks = 4):
self.fileName = fileName
self.fileSize = fileSize
self.numBlocks = numBlocks
try:
self.file = open(self.downname(), "rb+")
except:
self.file = open(self.downname(), "wb")
self.file.truncate(fileSize)
self.lock = Lock()
d, v = divmod(fileSize, PIECE_SIZE)
if v:
d += 1
self.numPieces = d
self.piecePad = 0
self.pieceCount = d // 8
if d % 8:
self.pieceCount + 1
self.piecePad = 8 - d % 8
self.pieceMap = bytearray(b"0"*d + b"1" * self.piecePad)
self.lastPieceSize = v
pb = self.numPieces // self.numBlocks
r = []
for i in range(self.numBlocks):
r.append(i * pb)
r.append(self.numPieces)
self.range = r
self.blocks = []
for i in range(self.numBlocks):
self.blocks.append(Block(self, self.range[i], self.range[i + 1], i == self.numBlocks - 1, self.lastPieceSize))
print("fileName:%s, size:%d, blocks:%d, pieces:%d, last:%d, range:%s"%(self.fileName, self.fileSize, self.numBlocks, self.numPieces, self.lastPieceSize, self.range))
self.load()
def write(self, pos, data):
wi
补充:Web开发 , Python ,