예제 #1
0
    def from_bytes(cls, data):
        if not check_bytes_length(data, 68):
            return None

        offset = 0
        offset, length = Network.read_byte_as_int(data, offset)
        offset, protocol = Network.read_bytes(data, 19, offset)
        offset, reserved = Network.read_bytes(data, 8, offset)
        offset, info_hash = Network.read_bytes(data, 20, offset)
        offset, peer_id = Network.read_bytes(data, 20, offset)

        result = cls(info_hash)
        result.length = length
        result.protocol = protocol
        result.reserved = reserved
        result.peer_id = peer_id

        return result
예제 #2
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 10):
            return None

        offset = 1
        offset, piece_index = Network.read_integer(data, offset)
        offset, data_offset = Network.read_integer(data, offset)
        offset, data_bytes = Network.read_bytes(data,
                                                len(data) - offset, offset)
        return cls(piece_index, data_offset, data_bytes)
예제 #3
0
    def from_bytes(cls, data):
        added = []
        dropped = []

        offset, payload_data = Network.read_bytes(data, len(data) - 2, 2)
        payload = Bencode.bdecode(payload_data)
        if b'added' in payload:
            addedbytes = payload[b'added']
            total_peers = int(len(addedbytes) / 6)
            offset = 0
            for index in range(total_peers):
                added.append(uri_from_bytes(addedbytes[offset:offset + 6]))
                offset += 6

        return cls(added, dropped)
예제 #4
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 2):
            return None

        offset, payload_data = Network.read_bytes(data, len(data) - 2, 2)
        payload = Bencode.bdecode(payload_data)

        metadata_message_type = payload[b'msg_type']
        piece_index = payload[b'piece']
        total_size = 0
        metadata = None
        if metadata_message_type == MetadataMessageType.Data:
            if b'total_size' not in payload:
                return None
            total_size = payload[b'total_size']
            metadata = data[2 + len(Bencode.bencode(payload)):len(data)]

        return cls(None, metadata_message_type, piece_index, total_size,
                   metadata)
예제 #5
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 3):
            return None

        offset, payload = Network.read_bytes(data, len(data) - 2, 2)
        return cls(payload)
예제 #6
0
    def from_bytes(cls, data):
        if not check_minimal_bytes_length(data, 2):
            return None

        offset, bitfield = Network.read_bytes(data, len(data) - 1, 1)
        return cls(bitfield)