def torrent(self): while not self.shutdown: self.pieces_left = len([m for m in self.pieces if not m['have']]) self.update_peers(self.query_trackers()) # Check whether we still have pieces left to send if not self.pieces_left: sys.stdout.write("\nDownload completed, cleaning up...\n") sys.exit(0) for peer_index, peer in enumerate(self.peers): # Process requests # TODO: Work on strategy for pieces that need be requested multiple times if peer.can_request(): for piece_index, piece in [(n, m) for (n, m) in enumerate(self.pieces) if not m['have']]: if peer.can_request() and peer.has_piece(piece_index) and not piece['requested']: peer.request_piece(piece_index, piece.get('size', self.piece_size)) piece['requested'] = True piece['attempts'] += 1 # Process completed pieces if peer.has_complete_pieces(): sys.stdout.write("\r%s/%s ({0:.2f}%%) pieces downloaded. Rate: %s/s". format((len(self.pieces) - self.pieces_left)/len(self.pieces)*100) % (len(self.pieces) - self.pieces_left, len(self.pieces), strmanip.parse_filesize(self.downloaded/(time.time() - self.runtime), 1000))) sys.stdout.flush() for piece in peer.complete_pieces(): if self.check_piece(piece['index'], piece['data']): self.write_piece(piece['index'], piece['data']) else: self.log.warn("Piece hash FAILED. Index: %s" % piece['index']) self.pieces[piece['index']]['requested'] = False sys.exit(0) Peer.loop() print("Exiting BitClient") sys.exit(1)
def update_peers(self, peer_info_list): '''Given a list of dictionaries, update the list of peers for this torrent''' if peer_info_list is not None: self.backup_peers.extend(peer_info_list) for peer_info in self.backup_peers: if self.add_peer(peer_info): del self.backup_peers[self.backup_peers.index(peer_info)] Peer.loop() # Check for anything to remove for index, peer in enumerate(self.peers): if peer.can_remove(): self.remove_peer(index)
def torrent(self): while not self.shutdown: self.pieces_left = len([m for m in self.pieces if not m['have']]) self.update_peers(self.query_trackers()) # Check whether we still have pieces left to send if not self.pieces_left: sys.stdout.write("\nDownload completed, cleaning up...\n") sys.exit(0) for peer_index, peer in enumerate(self.peers): # Process requests # TODO: Work on strategy for pieces that need be requested multiple times if peer.can_request(): for piece_index, piece in [ (n, m) for (n, m) in enumerate(self.pieces) if not m['have'] ]: if peer.can_request() and peer.has_piece( piece_index) and not piece['requested']: peer.request_piece( piece_index, piece.get('size', self.piece_size)) piece['requested'] = True piece['attempts'] += 1 # Process completed pieces if peer.has_complete_pieces(): sys.stdout.write( "\r%s/%s ({0:.2f}%%) pieces downloaded. Rate: %s/s". format((len(self.pieces) - self.pieces_left) / len(self.pieces) * 100) % (len(self.pieces) - self.pieces_left, len(self.pieces), strmanip.parse_filesize( self.downloaded / (time.time() - self.runtime), 1000))) sys.stdout.flush() for piece in peer.complete_pieces(): if self.check_piece(piece['index'], piece['data']): self.write_piece(piece['index'], piece['data']) else: self.log.warn("Piece hash FAILED. Index: %s" % piece['index']) self.pieces[piece['index']]['requested'] = False sys.exit(0) Peer.loop() print("Exiting BitClient") sys.exit(1)
def loop(): Peer.loop()