示例#1
0
文件: download.py 项目: romanz/pybt
    def handle(self, conn, event):

        if event.name in {'unchoke', 'choke'}:
            conn.state['am_choking'] = (event.name == 'choke')
            log.info('host is {}d by {}'.format(event.name, conn.name))

            metadata_request(conn, piece=0)
            conn.state['metadata'] = []
            return

        if event.name == 'extended':
            if event.cmd == peer.UT_METADATA:
                d, piece = bencode._decode(event.msg)

                if d['msg_type'] == 0:  # request
                    log.warning('unsupported')

                if d['msg_type'] == 1:  # data
                    log.info('got {} bytes of metadata from {}'.format(
                        len(piece), conn.name))
                    pieces = conn.state['metadata']
                    pieces.append(piece)
                    data = ''.join(pieces)
                    if self.info_hash == metainfo.hash(data):
                        return metadata_save(data)
                    else:
                        metadata_request(conn, piece=d['piece'] + 1)

                if d['msg_type'] == 2:  # reject
                    log.warning('no metadata')

            return
示例#2
0
文件: download.py 项目: romanz/pybt
    def handle(self, conn, event):

        if event.name in {"unchoke", "choke"}:
            conn.state["am_choking"] = event.name == "choke"
            log.info("host is {}d by {}".format(event.name, conn.name))

            metadata_request(conn, piece=0)
            conn.state["metadata"] = []
            return

        if event.name == "extended":
            if event.cmd == peer.UT_METADATA:
                d, piece = bencode._decode(event.msg)

                if d["msg_type"] == 0:  # request
                    log.warning("unsupported")

                if d["msg_type"] == 1:  # data
                    log.info("got {} bytes of metadata from {}".format(len(piece), conn.name))
                    pieces = conn.state["metadata"]
                    pieces.append(piece)
                    data = "".join(pieces)
                    if self.info_hash == metainfo.hash(data):
                        return metadata_save(data)
                    else:
                        metadata_request(conn, piece=d["piece"] + 1)

                if d["msg_type"] == 2:  # reject
                    log.warning("no metadata")

            return
示例#3
0
文件: tracker.py 项目: romanz/pybt
def test_tracker(arg):
    logging.basicConfig(
        format='%(asctime)-15s [%(levelname)s] %(message)s', 
        level=logging.DEBUG)
    
    meta = metainfo.MetaInfo(arg)
    peers = get_peers(meta, peer_id=metainfo.hash('test_tracker'), port=6889, timeout=10)
    print peers
示例#4
0
文件: download.py 项目: romanz/pybt
def metadata_save(data):
    info_hash = metainfo.hash(data)
    h = binascii.hexlify(info_hash)
    log.info('saving metadata for {}'.format(h))
    with file(h + '.meta', 'wb') as f:
        f.write(data)

    meta = metadata_load(info_hash)
    assert meta is not None  # verify correct data is written to file
    return meta
示例#5
0
文件: download.py 项目: romanz/pybt
def metadata_save(data):
    info_hash = metainfo.hash(data)
    h = binascii.hexlify(info_hash)
    log.info("saving metadata for {}".format(h))
    with file(h + ".meta", "wb") as f:
        f.write(data)

    meta = metadata_load(info_hash)
    assert meta is not None  # verify correct data is written to file
    return meta
示例#6
0
文件: download.py 项目: romanz/pybt
def metadata_load(info_hash):
    h = binascii.hexlify(info_hash)
    fname = h + ".meta"
    if not os.path.exists(fname):
        return None

    with file(fname, "rb") as f:
        data = f.read()

    if metainfo.hash(data) == info_hash:
        meta = metainfo.MetaInfo(bencode.decode(data))
        log.info('MetaInfo "{}" ({} hashes) = {:.1f}MB'.format(meta.name, len(meta.hashes), meta.total / 1e6))
        return meta
示例#7
0
文件: download.py 项目: romanz/pybt
def metadata_load(info_hash):
    h = binascii.hexlify(info_hash)
    fname = h + '.meta'
    if not os.path.exists(fname):
        return None

    with file(fname, 'rb') as f:
        data = f.read()

    if metainfo.hash(data) == info_hash:
        meta = metainfo.MetaInfo(bencode.decode(data))
        log.info('MetaInfo "{}" ({} hashes) = {:.1f}MB'.format(
            meta.name, len(meta.hashes), meta.total / 1e6))
        return meta
示例#8
0
文件: storage.py 项目: romanz/pybt
    def validate(self, index_list=None):
        if index_list is None:
            index_list = indices(~self.bits)

        for i in index_list:
            data = self.read(i)
            h = metainfo.hash(data)
            success = (self.meta.hashes[i] == h)
            self.bits[i] = success
            if success:
                log.debug('validated piece #{} ({} of {})'.format(i, self.bits.count(), len(self.bits)))

        log.info('host has {} of {} pieces'.format(self.bits.count(), len(self.bits)))
        return sum(self.bits[i] for i in index_list)
示例#9
0
文件: storage.py 项目: romanz/pybt
    def validate(self, index_list=None):
        if index_list is None:
            index_list = indices(~self.bits)

        for i in index_list:
            data = self.read(i)
            h = metainfo.hash(data)
            success = (self.meta.hashes[i] == h)
            self.bits[i] = success
            if success:
                log.debug('validated piece #{} ({} of {})'.format(
                    i, self.bits.count(), len(self.bits)))

        log.info('host has {} of {} pieces'.format(self.bits.count(),
                                                   len(self.bits)))
        return sum(self.bits[i] for i in index_list)
示例#10
0
文件: test.py 项目: romanz/pybt
def main(args):
    logging.basicConfig(
        format='%(asctime)-15s [%(levelname)s] %(name)s: %(message)s', 
        level=logging.INFO)

    host_id = metainfo.hash(args.host)

    m = metainfo.parse_magnet(args.link)
    meta = download.metadata_load(m['info_hash'])

    if args.metadata:
        if meta is None:
            dl = download.Metadata(host_id, m['info_hash'])
            meta = dl.run(m['trackers'])

    if args.torrent and meta:
        dl = download.Torrent(host_id, meta)
        if not all(dl.data.bits):
            dl.run(m['trackers'])
        log.info('Download completed')
示例#11
0
def main(args):
    logging.basicConfig(
        format='%(asctime)-15s [%(levelname)s] %(name)s: %(message)s',
        level=logging.INFO)

    host_id = metainfo.hash(args.host)

    m = metainfo.parse_magnet(args.link)
    meta = download.metadata_load(m['info_hash'])

    if args.metadata:
        if meta is None:
            dl = download.Metadata(host_id, m['info_hash'])
            meta = dl.run(m['trackers'])

    if args.torrent and meta:
        dl = download.Torrent(host_id, meta)
        if not all(dl.data.bits):
            dl.run(m['trackers'])
        log.info('Download completed')