Пример #1
0
 def datagramReceived(self, datagram, node_addr):
     try:
         data = bdecode(datagram)
     except BTError:
         return
     if data['y'] == 'q':
         self._KRPC_recieve_Query(data, node_addr)
     elif data['y'] == 'r':
         self._KRPC_recieve_response(data, node_addr)
     elif data['y'] == 'e':
         self._KRPC_recieve_error(data, node_addr)
     else:
         assert False
Пример #2
0
 def render_GET(self, request):
     d = bencode.bdecode(open('./torrents/data.torrent').read())
     d['announce'] = 'http://'+hostname+':'+str(tracker_port)+'/announce'
     torrent = bencode.bencode(d)
     return torrent
Пример #3
0
    def __init__(self, path=None, meta_info=None):
        if path:
            ct = open(path, 'rb').read()
            metainfo = bdecode(ct)
        elif meta_info:
            metainfo = meta_info
        else:
            raise Exception("Must pass either a BT meta file path or the " +\
                "meta  info itself!")
        self.metainfo = metainfo

        if 'announce' in metainfo:
            self.announce_list = [metainfo['announce']]
            if 'announce-list' in metainfo:
                self.announce_list += reduce(lambda x,y: x+y, metainfo['announce-list'])
        else: # Trackerless torrent?
            self.announce_list = []

        if 'encoding' in metainfo:
            self.encoding = metainfo['encoding']
            
        info = metainfo['info']
        temp = hashlib.sha1(bencode(info))
        self.info_hash =  temp.digest()
        self.pretty_info_hash =  temp.hexdigest()

        self.piece_length = info['piece length']

        hashes = info['pieces']
        self.pieces_hash = [hashes[i:i+20] for i in range(0, len(hashes), 20)]
        self.pieces_size = len(self.pieces_hash)
        
        self.files = []

        self.topDir = '.'
        name = info['name'].decode(self.encoding)
        if 'files' in info:
            cur_size = 0
            for fd in info['files']:
                _d = fd.copy()
                _path = [name] + [p.decode(self.encoding) for p in _d['path']]
                _path = os.path.join(*_path)
                _d['path'] = _path
                _start = cur_size
                _stop = cur_size + _d['length']
                cur_size = _stop
                _d['pos_range'] = _start, _stop
                self.files.append(_d)
                
            self.total_length = cur_size                
            self.topDir = name
                
        else:
            _d = {}
            _d['path'] = name
            _d['length'] = info['length']
            _d['pos_range'] = 0, info['length'] # TODO: Is this right?
            self.files.append(_d)
            self.total_length = info['length']

        last_piece_length = self.total_length % self.piece_length
        if last_piece_length == 0 :
            last_piece_length = self.piece_length
        self.last_piece_length = last_piece_length