def Main(): name = input("Enter your name please: ") newPeer = Peer(name, portNumber=5600, allowConnection=True) newPeer.connect(justGetID=False) print("You are now connected to the server!") print(f"Hello {newPeer.name}! Your ID is {newPeer.id}") filename = input("Please enter the filename: ") newPeer.requestFile(filename)
def main(): torrent = Torrent('data/ubuntu.torrent') td = deepcopy(torrent.torrent_file_dict) td[b'info'][b'pieces'] = '20 byte pieces hashes go here...' pp(td) print('Number of pieces in the file:', torrent.num_pieces) print('Size of download in bytes: ', torrent.get_download_length()) input() peers_info = PeersGetter(torrent).parse_peers() writers = [] readers = [] peers_connected_to_count = 0 # Build connected peers for peer_info in peers_info: if peers_connected_to_count > 3: break peers_connected_to_count += 1 peer = Peer(torrent, peer_info=peer_info) peer.connect() if peer.is_connected: writers.append(peer) readers.append(peer) while not torrent.is_download_finished(): # print( # 'Downloading... Writers: {} Readers: {}'.format( # len(readers), len(writers) # ) # ) to_read, to_write, errors = select.select(readers, writers, readers) for peer in to_read: peer.read() for peer in to_write: peer.write() for peer in errors: readers.remove(peer) writers.remove(peer)
def _try_peers(self, peers): """Loops through the list of peers and tries to connect to them (only one at a time right now). TODO: We're currently connecting to multiple peers, but not coordinating the work we send well. """ for peer_entry in peers: if peer_entry['id'] == self.peer_id: continue print('Trying peer: {}'.format(peer_entry)) peer = Peer(peer_entry['id'], peer_entry['ip'], peer_entry['port'], self._torrent) try: peer.connect(self.peer_id) except PeerConnectionError: continue else: self._peers.append(peer) peer.subscribe_for_messages_to_client( self.peer_message_receiver(peer))