def test_dict(self): self.assertEqual(bencode({'a':2}), 'd1:ai2ee') self.assertEqual(bencode({'b':[1,2]}), 'd1:bli1ei2eee') self.assertEqual(bencode({'cow':'moo', 'spam':'eggs'}), 'd3:cow3:moo4:spam4:eggse') self.assertEqual(bencode({'spam':['a', 'b']}), 'd4:spaml1:a1:bee') self.assertEqual(bencode({"publisher":"bob", "publisher-webpage":"www.example.com", "publisher.location":"home" }), 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee')
def test_list(self): self.assertEqual(bencode(['a', 'c', 'e']), 'l1:a1:c1:ee') self.assertEqual(bencode([1,2,3]), 'li1ei2ei3ee') self.assertEqual(bencode([1,'a',3]), 'li1e1:ai3ee') self.assertEqual(bencode([[1,2],['a','b']]), 'lli1ei2eel1:a1:bee') self.assertEqual(bencode(['spam', 'eggs']), 'l4:spam4:eggse')
def test_list(self): self.assertEqual(bencode(['a', 'c', 'e']), 'l1:a1:c1:ee') self.assertEqual(bencode([1, 2, 3]), 'li1ei2ei3ee') self.assertEqual(bencode([1, 'a', 3]), 'li1e1:ai3ee') self.assertEqual(bencode([[1, 2], ['a', 'b']]), 'lli1ei2eel1:a1:bee') self.assertEqual(bencode(['spam', 'eggs']), 'l4:spam4:eggse')
def clean_torrent(self): torrent = self.cleaned_data["torrent"] # Check that the torrent is actually a torrent file and set the info_hash _, ext = os.path.splitext(torrent.name) if ext != '.torrent': raise forms.ValidationError('File must end with .torrent') try: data = bdecode(torrent.read()) except ValueError: raise forms.ValidationError('Problem parsing torrent file') if 'info' not in data: raise forms.ValidationError('Info dict not in torrent file') #Verify uniqueness the_hash = hashlib.sha1(bencode(data['info'])).hexdigest() if Torrent.objects.filter(info_hash=the_hash).count() != 0: raise forms.ValidationError('Torrent already exists [%s]' % the_hash) #encode the name torrent.name = '%s.torrent' % hashlib.sha1( the_hash + settings.SECRET_KEY).hexdigest() return torrent
def _calculate_properties(self): self.announce = self._query('announce') try: announce_list = self._query('announce-list') except KeyError: announce_list = [] self.all_announce_urls = [self.announce] + \ self._parse_announce_list(announce_list) self.info = self._query('info') self.have = [0] * self.num_pieces self.frequency = [0] * self.num_pieces self.hashed_info = sha1(bencode(self.info)).digest() self.piece_length = self._query('piece length') pieces = self._query('pieces') self.piece_hashes = [pieces[i:i+20] for i in range(0, len(pieces), 20)] self.num_pieces = len(self.piece_hashes) self.file_mode = 'multi' if 'files' in self.info else 'single' if self.file_mode == 'single': self.files = [{'length': int(self.query('length')), 'path': [self._query('name')]}] else: self.files = [{'length': int(f['length']), 'path': f['path']} for f in self._query('files')] self.total_length = sum(f['length'] for f in self.files)
def test_dict(self): self.assertEqual(bencode({'a': 2}), 'd1:ai2ee') self.assertEqual(bencode({'b': [1, 2]}), 'd1:bli1ei2eee') self.assertEqual(bencode({ 'cow': 'moo', 'spam': 'eggs' }), 'd3:cow3:moo4:spam4:eggse') self.assertEqual(bencode({'spam': ['a', 'b']}), 'd4:spaml1:a1:bee') self.assertEqual( bencode({ "publisher": "bob", "publisher-webpage": "www.example.com", "publisher.location": "home" }), 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee' )
def download_torrent(request, object_id): torrent = get_object_or_404(Torrent, pk=object_id) #load the file into memory data = torrent.torrent.read() decoded_data = bdecode(data) #set the tracker, reverse announce url announce_url = 'http://%s%s' % (Site.objects.get_current().domain, reverse('tracker.views.announce')) decoded_data['announce'] = announce_url #generate a response with correct mimetype and file as the attachment response = HttpResponse(mimetype='application/x-bittorrent') response['Content-Disposition'] = 'attachment; filename=%s.torrent' % torrent.info_hash response.write(bencode(decoded_data)) return response
def download_torrent(request, object_id): torrent = get_object_or_404(Torrent, pk=object_id) #load the file into memory data = torrent.torrent.read() decoded_data = bdecode(data) #set the tracker, reverse announce url announce_url = 'http://%s%s' % (Site.objects.get_current().domain, reverse('tracker.views.announce')) decoded_data['announce'] = announce_url #generate a response with correct mimetype and file as the attachment response = HttpResponse(mimetype='application/x-bittorrent') response[ 'Content-Disposition'] = 'attachment; filename=%s.torrent' % torrent.info_hash response.write(bencode(decoded_data)) return response
def clean_torrent(self): torrent = self.cleaned_data["torrent"] # Check that the torrent is actually a torrent file and set the info_hash _, ext = os.path.splitext(torrent.name) if ext != '.torrent': raise forms.ValidationError('File must end with .torrent') try: data = bdecode(torrent.read()) except ValueError: raise forms.ValidationError('Problem parsing torrent file') if 'info' not in data: raise forms.ValidationError('Info dict not in torrent file') #Verify uniqueness the_hash = hashlib.sha1(bencode(data['info'])).hexdigest() if Torrent.objects.filter(info_hash=the_hash).count() != 0: raise forms.ValidationError('Torrent already exists [%s]' % the_hash) #encode the name torrent.name = '%s.torrent' % hashlib.sha1(the_hash + settings.SECRET_KEY).hexdigest() return torrent
def test_reciprical(self): for i in xrange(100): element = self.rand_element() self.assertEqual(bdecode(bencode(element)), element)
def test_dict_sorting(self): self.assertEqual(bencode({'a':1,'c':10,'b':5}), 'd1:ai1e1:bi5e1:ci10ee')
def test_int(self): self.assertEqual(bencode(1), 'i1e') self.assertEqual(bencode(1832), 'i1832e') self.assertEqual(bencode(3), 'i3e')
def test_dict_sorting(self): self.assertEqual(bencode({ 'a': 1, 'c': 10, 'b': 5 }), 'd1:ai1e1:bi5e1:ci10ee')
def test_str(self): self.assertEqual(bencode('abc'), '3:abc') self.assertEqual(bencode('aasdfbc'), '7:aasdfbc') self.assertEqual(bencode('spam'), '4:spam')