示例#1
0
 def render_GET(self, request):
     d = {}
     d = {'files':{
                   '':{'complete':1,
                       'downloaded':1,
                       'incomplete':0}}}
     return bencode.bencode(d)
示例#2
0
def save_meta_info(path, meta_info):
    """Bencodes and saves the meta_info dictioary to the path provided.

    Warning: This does not verify the meta_info for correctness.
    """
    # TODO: Check/verify meta_info?
    target_file = open(path, 'wb')
    target_file.write(bencode(meta_info))
    target_file.close()
示例#3
0
 def render_GET(self, request):
     #TODO implement a tracker!
     d = {}
     d['peers'] = ''.join(chr(int(x)) for x in ip.split('.')) + chr(client_port // 256) + chr(client_port % 256)
     d['interval'] = 10
     d['tracker_id'] = 'asdf'
     d['complete'] = 1
     d['incomplete'] = 1
     return bencode.bencode(d)
示例#4
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
示例#5
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
示例#6
0
 def _KRPC_send_error(self, node_addr, t_id, error):
     data = {'t' : t_id,
             'y' : 'e',
             'e' : error}
     self.writeDatagram(bencode(data), node_addr)
示例#7
0
 def _KRPC_send_response(self, node_addr, t_id, args):
     response = {'t' : t_id,
                 'y' : 'r',
                 'r' : args}
     self.writeDatagram(bencode(response), node_addr)
示例#8
0
 def _KRPC_send_query(self, node_addr, t_id, qtype, args):
     data = {'t' : t_id,
             'y' : 'q',
             'q' : qtype,
             'a' : args }
     self.writeDatagram(bencode(data), node_addr)