def parse_peers_list(data): if isinstance(data, bytes): if len(data) % 6 != 0: raise ValueError( 'Invalid length of a compact representation of peers') return list(map(Peer.from_compact_form, slice(data, 6))) else: return list(map(Peer.from_dict, data))
def __init__(self, info): self.info = info self.piece_length = info["piece length"] self.length = info["length"] self.piece_num = (self.length - 1) / self.piece_length + 1 self.data_db = {} self.comp_db = {} self.sha1 = slice(info["pieces"], 20) self.downloaded_size = 0
def info_discionario(file): with open(file) as f: contents = f.read() # varia de acordo com o tamanho do arquivo tamanho_pedaco = 524288 # objeto info info = {} # adicionando informações info["tamanho_pedaco"] = tamanho_pedaco info["tamanho"] = len(contents) info["nome"] = file info["md5sum"] = md5(contents).hexdigest() # Generate the pieces pieces = slice(contents, tamanho_pedaco) pieces = [ sha1(p).digest() for p in pieces ] info["pieces"] = collapse(pieces) return info
def from_dict(cls, dictionary): info_hash = hashlib.sha1(bencode(dictionary)).digest() if len(dictionary[b"pieces"]) % SHA1_DIGEST_LEN != 0: raise ValueError("Invalid length of pieces string") piece_hashes = slice(dictionary[b"pieces"], SHA1_DIGEST_LEN) if b"files" in dictionary: files = list(map(FileInfo.from_dict, dictionary[b"files"])) else: files = [FileInfo.from_dict(dictionary)] return cls(info_hash, dictionary[b"piece length"], piece_hashes, dictionary[b"name"].decode(), files, private=dictionary.get(b"private", False))
def make_info_dict(file): """ Returns the info dictionary for a torrent file. """ with open(file) as f: contents = f.read() piece_length = 524288 # TODO: This should change dependent on file size info = {} info["piece length"] = piece_length info["length"] = len(contents) info["name"] = file info["md5sum"] = md5(contents).hexdigest() # Generate the pieces pieces = slice(contents, piece_length) pieces = [sha1(p).digest() for p in pieces] info["pieces"] = ''.join([p for p in pieces]) return info
def make_info_dict(file): """ Returns the info dictionary for a torrent file. """ with open(file) as f: contents = f.read() piece_length = 524288 # TODO: This should change dependent on file size info = {} info["piece length"] = piece_length info["length"] = len(contents) info["name"] = file info["md5sum"] = md5(contents).hexdigest() # Generate the pieces pieces = slice(contents, piece_length) pieces = [ sha1(p).digest() for p in pieces ] info["pieces"] = collapse(pieces) return info
def load_initial_pieces(self): self.piece_size = self.data["info"]["piece length"] self.num_pieces = int( ceil((self.data["info"]["length"] + 0.000000) / self.piece_size)) #print self.data["info"]["length"], self.num_pieces self.pieces_bitmap = self.num_pieces * bitarray('0') try: f = open(self.data["info"]["name"], "r") except IOError: self.seeder = False self.pieces_data = [None for i in range(self.num_pieces)] self.pieces_count = 0 self.left = self.data["info"]["length"] else: self.seeder = True self.pieces_data = slice(f.read(), self.piece_size) f.close() self.pieces_bitmap.setall('1') self.pieces_count = self.num_pieces self.left = 0 finally: self.loading_initial_peers_setup()
def make_info_dict(file): """ Returns the info dictionary for a torrent file. """ with open(file) as f: contents = f.read() piece_length = find_piece_length(len(contents)) print 'File length "%s", using piece length "%s"' %(len(contents), piece_length,) info = {} info["piece length"] = piece_length info["length"] = len(contents) info["name"] = file info["md5sum"] = md5(contents).hexdigest() # Generate the pieces pieces = slice(contents, piece_length) pieces = [ sha1(p).digest() for p in pieces ] info["pieces"] = collapse(pieces) return info
def decode_binary_peers(peers): """ Return a list of IPs and ports, given a binary list of peers, from a tracker response. """ peers = slice(peers, 6) # Cut the response at the end of every peer return [(socket.inet_ntoa(p[:4]), decode_port(p[4:])) for p in peers]
def test_too_long(self): """ Test that a string too long works fine. """ self.n = util.slice("abcd", 6) self.assertEqual(self.n, ["abcd"])
def test_longer(self): """ Test that a larger string slice works correctly. """ self.n = util.slice("abcdef", 2) self.assertEqual(self.n, ["ab", "cd", "ef"])
def test_simple(self): """ Test that a small string slices correctly. """ self.n = util.slice("abc", 1) self.assertEqual(self.n, ["a", "b", "c"])