def test_dict(self): ben_string = (b'd5:hello5:world2:hil5:hello5:worlde3:' b'hi2l5:hello5:worldee') data = { b'hello': b'world', b'hi2': [b'hello', b'world'], b'hi': [b'hello', b'world'] } self.assertEqual(encode(data), ben_string) self.assertEqual(decode(ben_string), data)
def announce(self, params): """ Announce response. Returns bencoded dictionary """ peer = Peer(params) info_hash = params['info_hash'] try: torrent = self.get_torrent_by_hash(info_hash) torrent.add_peer(peer) peers = torrent.get_peers() response = { b'interval': self.cfg['check_interval'], # b'tracker id': b'WarpTracker', # b'complete': len([p for p in peers if p.is_seeder]), # b'incomplete': len([p for p in peers if p.is_leecher]), b'peers': b''.join([p.as_bytes_compact for p in peers]) } except InfoHashNotFound: response = { b'failure reason': b'Torrent not registered', b'failure code': 200 } logger.debug('Response: %s', bencode.encode(response)) return bencode.encode(response)
def test_zero_string(self): ben_string = b'0:' data = b'' self.assertEqual(encode(data), ben_string) self.assertEqual(decode(ben_string), data)
def test_list(self): ben_string = b'l5:hello5:worlde' data = [b'hello', b'world'] self.assertEqual(encode(data), ben_string) self.assertEqual(decode(ben_string), data)
def test_negative_int(self): ben_string = b'i-1e' data = -1 self.assertEqual(encode(data), ben_string) self.assertEqual(decode(ben_string), data)
def test_int(self): ben_string = b'i42e' data = 42 self.assertEqual(encode(data), ben_string) self.assertEqual(decode(ben_string), data)
def test_string(self): ben_string = b'12:Hello World!' data = b'Hello World!' self.assertEqual(encode(data), ben_string) self.assertEqual(decode(ben_string), data)
def bencoded_info(self): """ Bencoded info block """ return bencode.encode(self.meta_data[b'info'])
def bencoded_meta_data(self): """ Return meta file content """ return bencode.encode(self.meta_data)