def __init__(self, peer_info, session_data, max_requests = 1): '''Initialize a peer object given a dictionary with port and ip as well as a handshake and info_hash''' asyncore.dispatcher.__init__(self) self.peer_info = (peer_info['ip'], int(peer_info['port'])) self.handshake, self.info_hash = session_data['handshake'], session_data['info_hash'] self.log = bitlog.log() self.create_socket() self.status_ok = True self.sent_prepare = False self.removable = False self.lastcall = b"" self.writebuffer = b"" self.handshaked = False self.is_choked = True self.choked = True self.is_interested = False self.interested = False self.max_requests = max_requests self.log.debug("Connecting %s:%s" % self.peer_info) try: self.connect(self.peer_info) except (ConnectionRefusedError, OSError) as e: self.disconnect() self.pending_requests = 0 self.bitfield = {} self.block = [] self.piece_buffer = {} # pending self.piece_completed = [] # done
def __init__(self, peer_info, session_data, max_requests=1): '''Initialize a peer object given a dictionary with port and ip as well as a handshake and info_hash''' asyncore.dispatcher.__init__(self) self.peer_info = (peer_info['ip'], int(peer_info['port'])) self.handshake, self.info_hash = session_data[ 'handshake'], session_data['info_hash'] self.log = bitlog.log() self.create_socket() self.status_ok = True self.sent_prepare = False self.removable = False self.lastcall = b"" self.writebuffer = b"" self.handshaked = False self.is_choked = True self.choked = True self.is_interested = False self.interested = False self.max_requests = max_requests self.log.debug("Connecting %s:%s" % self.peer_info) try: self.connect(self.peer_info) except (ConnectionRefusedError, OSError) as e: self.disconnect() self.pending_requests = 0 self.bitfield = {} self.block = [] self.piece_buffer = {} # pending self.piece_completed = [] # done
def __init__(self, filename): '''Initialize a torrent object, given the filename''' self.good_status = True self.log = bitlog.log() self.filename = filename self.filepath = os.path.dirname(self.filename) self.torrentname = self.filename[len(self.filepath):] self.total_size = 0 self.left = 0 self.metainfo_data = self.read_torrent_file(filename) # if 'encoding' in self.metainfo_data: # self.log.warn("Attention: Torrent has specified encoding") # sys.exit(0) self.info = self.metainfo_data['info'] self.piece_size = self.info['piece length'] try: self.partfiledata = self.parse_partfile_data( self.filepath, self.info['name'] if 'name' in self.info else os.path.basename(self.filename), self.info['length'] if 'length' in self.info else self.info['files']) except KeyError: self.good_status = False print( "MalformedTorrent: The length key for torrent %s was not given, removing from queue" % self.filename) self.peerid = self.get_peer_id() #TODO: realistically, on startup we should recognize whether files have # finished downloading self.info_hash = self.get_info_hash(self.info) self.trackers = {} self.parse_trackers(self.metainfo_data) self.pieces = [] self.map_pieces(self.info['pieces']) self.peers = [] # Peer objects self.backup_peers = [] # list of {ip:,port:} dicts for backup self.uploaded = 0 self.finished = False self.downloaded = 0 self.pieces_left = 0 self.runtime = time.time() self.shutdown = False
def __init__(self,filename): '''Initialize a torrent object, given the filename''' self.good_status = True self.log = bitlog.log() self.filename = filename self.filepath = os.path.dirname(self.filename) self.torrentname = self.filename[len(self.filepath):] self.total_size = 0 self.left = 0 self.metainfo_data = self.read_torrent_file(filename) # if 'encoding' in self.metainfo_data: # self.log.warn("Attention: Torrent has specified encoding") # sys.exit(0) self.info = self.metainfo_data['info'] self.piece_size = self.info['piece length'] try: self.partfiledata = self.parse_partfile_data(self.filepath, self.info['name'] if 'name' in self.info else os.path.basename(self.filename), self.info['length'] if 'length' in self.info else self.info['files']) except KeyError: self.good_status = False print("MalformedTorrent: The length key for torrent %s was not given, removing from queue" % self.filename) self.peerid = self.get_peer_id() #TODO: realistically, on startup we should recognize whether files have # finished downloading self.info_hash = self.get_info_hash(self.info) self.trackers = {};self.parse_trackers(self.metainfo_data) self.pieces = []; self.map_pieces(self.info['pieces']) self.peers = [] # Peer objects self.backup_peers = [] # list of {ip:,port:} dicts for backup self.uploaded = 0 self.finished = False self.downloaded = 0 self.pieces_left = 0 self.runtime = time.time() self.shutdown = False