Exemplo n.º 1
0
    def receive_announce(self, data):
        interval, leechers, seeders = struct.unpack('!III', data[:12])
        num_peers = seeders + leechers
        peers = []

        for chunk in utils.grouper(6, data[12:12 + 6 * num_peers]):
            peers.append(Peer(*utils.unpack_peer_address(''.join(chunk))))

        return TrackerResponse(peers, interval)
Exemplo n.º 2
0
    def get_peers(self, data):
        peers = data['peers']

        if isinstance(peers, list):
            for peer_dict in peers:
                yield Peer(peer_dict['ip'], peer_dict['port'], peer_dict['peer_id'])
        else:
            for chunk in utils.grouper(6, peers):
                yield Peer(*utils.unpack_peer_address(''.join(chunk)))
Exemplo n.º 3
0
    def get_peers(self, data):
        peers = data['peers']

        if isinstance(peers, list):
            for peer_dict in peers:
                yield Peer(peer_dict['ip'], peer_dict['port'],
                           peer_dict['peer_id'])
        else:
            for chunk in utils.grouper(6, peers):
                yield Peer(*utils.unpack_peer_address(''.join(chunk)))
Exemplo n.º 4
0
    def pack_body(self):
        data = ''
        bits = ['0'] * (max(self.bitfield.keys()) + 1)

        for piece, state in self.bitfield.items():
            bits[piece] = '1' if state else '0'

        for chunk in utils.grouper(8, bits, fillvalue='0'):
            data += chr(int(''.join(chunk), 2))

        return data
Exemplo n.º 5
0
    def pack_body(self):
        data = ''
        bits = ['0'] * (max(self.bitfield.keys()) + 1)

        for piece, state in self.bitfield.items():
            bits[piece] = '1' if state else '0'

        for chunk in utils.grouper(8, bits, fillvalue='0'):
            data += chr(int(''.join(chunk), 2))

        return data