Example #1
0
 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)
Example #2
0
    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)
Example #3
0
 def test_zero_string(self):
     ben_string = b'0:'
     data = b''
     self.assertEqual(encode(data), ben_string)
     self.assertEqual(decode(ben_string), data)
Example #4
0
 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)
Example #5
0
 def test_negative_int(self):
     ben_string = b'i-1e'
     data = -1
     self.assertEqual(encode(data), ben_string)
     self.assertEqual(decode(ben_string), data)
Example #6
0
 def test_int(self):
     ben_string = b'i42e'
     data = 42
     self.assertEqual(encode(data), ben_string)
     self.assertEqual(decode(ben_string), data)
Example #7
0
 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)
Example #8
0
 def bencoded_info(self):
     """ Bencoded info block """
     return bencode.encode(self.meta_data[b'info'])
Example #9
0
 def bencoded_meta_data(self):
     """ Return meta file content """
     return bencode.encode(self.meta_data)