Esempio n. 1
0
 def test_list(self):
     self.assertEqual(bencode.decode('le'), [])
     self.assertEqual(bencode.decode('ll4:testi2eell3:fooeli3eeee'), [
         ['test', 2], [
             ['foo'], [3]
         ]
     ])
Esempio n. 2
0
    def test_dict(self):
        self.assertEqual(bencode.decode('de'), {})
        self.assertEqual(
            bencode.decode('d3:fool3:bard4:testl5:againi12eeee4:testi12ee'), {
                'test': 12,
                'foo': ['bar', {
                    'test': ['again', 12]
                }]
            })

        self.assertRaises(ValueError, bencode.decode, 'di1ei1ee')
Esempio n. 3
0
    def test_dict(self):
        self.assertEqual(bencode.decode('de'), {})
        self.assertEqual(bencode.decode('d3:fool3:bard4:testl5:againi12eeee4:testi12ee'), {
            'test': 12,
            'foo': [
                'bar',
                {'test': ['again', 12]}
            ]
        })

        self.assertRaises(ValueError, bencode.decode, 'di1ei1ee')
Esempio n. 4
0
    def announce(self, peer_id, port, event=None, num_wanted=None, compact=True, no_peer_id=None):
        params = {
            'info_hash': self.torrent.info_hash(),
            'peer_id': peer_id,
            'port': port,
            'uploaded': self.torrent.uploaded,
            'downloaded': self.torrent.downloaded,
            'left': self.torrent.remaining,
            'compact': int(compact)
        }

        if num_wanted is not None:
            params['numwant'] = num_wanted

        if compact is not None:
            params['compact'] = int(bool(compact))

        if no_peer_id is not None:
            params['no_peer_id'] = int(bool(no_peer_id))

        if event is not None:
            params['event'] = event

        tracker_url = url_concat(self.url, params)

        response = yield self.client.fetch(tracker_url)
        decoded_body = bencode.decode(response.body)

        if 'failure reason' in decoded_body:
            raise TrackerFailure(decoded_body['failure reason'])

        peers = list(self.get_peers(decoded_body))
        result = TrackerResponse(peers, decoded_body['interval'])

        raise Return(result)
Esempio n. 5
0
    def announce(self,
                 peer_id,
                 port,
                 event=None,
                 num_wanted=None,
                 compact=True,
                 no_peer_id=None):
        params = {
            'info_hash': self.torrent.info_hash(),
            'peer_id': peer_id,
            'port': port,
            'uploaded': self.torrent.uploaded,
            'downloaded': self.torrent.downloaded,
            'left': self.torrent.remaining,
            'compact': int(compact)
        }

        if num_wanted is not None:
            params['numwant'] = num_wanted

        if compact is not None:
            params['compact'] = int(bool(compact))

        if no_peer_id is not None:
            params['no_peer_id'] = int(bool(no_peer_id))

        if event is not None:
            params['event'] = event

        tracker_url = url_concat(self.url, params)

        response = yield self.client.fetch(tracker_url)
        decoded_body = bencode.decode(response.body)

        if 'failure reason' in decoded_body:
            raise TrackerFailure(decoded_body['failure reason'])

        peers = list(self.get_peers(decoded_body))
        result = TrackerResponse(peers, decoded_body['interval'])

        raise Return(result)
Esempio n. 6
0
 def test_list(self):
     self.assertEqual(bencode.decode('le'), [])
     self.assertEqual(bencode.decode('ll4:testi2eell3:fooeli3eeee'),
                      [['test', 2], [['foo'], [3]]])
Esempio n. 7
0
 def test_int(self):
     self.assertEqual(bencode.decode('i12e'), 12)
     self.assertEqual(bencode.decode('i-12e'), -12)
     self.assertEqual(bencode.decode('i0e'), 0)
Esempio n. 8
0
 def test_string(self):
     self.assertEqual(bencode.decode('4:test'), 'test')
Esempio n. 9
0
 def test_int(self):
     self.assertEqual(bencode.decode('i12e'), 12)
     self.assertEqual(bencode.decode('i-12e'), -12)
     self.assertEqual(bencode.decode('i0e'), 0)
Esempio n. 10
0
 def test_string(self):
     self.assertEqual(bencode.decode('4:test'), 'test')