def _load_torrent(self, content): #f = open(path, 'rb') #content = f.read() #f.close() parsed = bdecoder.decode(content) info_hash = hashlib.sha1(bdecoder.decode(content, 2)[b'info']) self._hex_info_hash = info_hash.hexdigest() self._info_hash = info_hash.digest() self._announce = parsed[b'announce'].decode('UTF-8') if b'announce-list' in parsed: announce_list = parsed[b'announce-list'] self._announce_list = [] for tire in announce_list: self._announce_list.append(\ [announce.decode('UTF-8') for announce in tire]) else: self._announce_list = None info = parsed[b'info'] self._piece_length = info[b'piece length'] pieces = info[b'pieces'] self._pieces = [] current = 0 length = len(pieces) while current < length: self._pieces.append(pieces[current:current + 20]) current += 20 self._name = info[b'name'].decode('UTF-8') if b'files' in info: self._files = info[b'files'] self._mode = 'multiple' self._length = 0 for f in self._files: self._length += f[b'length'] else: self._length = info[b'length'] self._mode = 'single' self._nb_of_pieces = self._length // self._piece_length if self._length % self._piece_length != 0: self._nb_of_pieces += 1
def _load_torrent(self, content): # f = open(path, 'rb') # content = f.read() # f.close() parsed = bdecoder.decode(content) info_hash = hashlib.sha1(bdecoder.decode(content, 2)[b"info"]) self._hex_info_hash = info_hash.hexdigest() self._info_hash = info_hash.digest() self._announce = parsed[b"announce"].decode("UTF-8") if b"announce-list" in parsed: announce_list = parsed[b"announce-list"] self._announce_list = [] for tire in announce_list: self._announce_list.append([announce.decode("UTF-8") for announce in tire]) else: self._announce_list = None info = parsed[b"info"] self._piece_length = info[b"piece length"] pieces = info[b"pieces"] self._pieces = [] current = 0 length = len(pieces) while current < length: self._pieces.append(pieces[current : current + 20]) current += 20 self._name = info[b"name"].decode("UTF-8") if b"files" in info: self._files = info[b"files"] self._mode = "multiple" self._length = 0 for f in self._files: self._length += f[b"length"] else: self._length = info[b"length"] self._mode = "single" self._nb_of_pieces = self._length // self._piece_length if self._length % self._piece_length != 0: self._nb_of_pieces += 1
def _parse_response(self, resp): parsed = None try: parsed = bdecoder.decode(resp) except BencodeError as err: raise TrackerResponseError("Response parsing failed. %s" % err) if b'failure reason' in parsed: _logger.info("Tracker %s failure, with %s" %\ (self._tracker.announce, parsed[b'failure'])) raise TrackerResponseError(\ parsed[b'failure reason'].decode('UTF-8')) self._interval = parsed[b'interval'] self._min_interval = parsed.get(b'min interval', 0) self._tracker_id = parsed.get(b'tracker id', None) self._complete = parsed[b'complete'] self._incomplete = parsed[b'incomplete'] peers = parsed[b'peers'] if isinstance(peers, list): self._peers = peers else: if len(peers) % 6 != 0: _logger.info("Invalid peer list length from tracker %s" \ % self._tracker.announce) raise TrackerResponseError("Peer list corrupted") self._peers = [] start = 0 while start < len(peers): ip = peers[start:start + 4] ip = socket.inet_ntoa(ip) port = peers[start + 4:start + 6] port, = struct.unpack('!H', port) self._peers.append({b'ip': ip, b'port': port}) start += 6