def _handle_write_packet(self, packet):
        """Takes a packet from the client and advances the state machine
        depending on that packet. This should only be invoked from the WRITING
        state. If given the last packet in a data transfer (bytes of data is
        less than 512), then invokes the application level action with all of
        the data from the conversation.

        Args:
            packet: A packet object that has already been unpacked.

        Returns:
            An appropriate AcknowledgementPacket containing a matching block
            number.
        """
        assert self.state == WRITING
        if not isinstance(packet, packets.DataPacket):
            return packets.ErrorPacket(0, "Illegal packet type given"
                                          " current state of conversation")
        # Add one because acknowledgements are always behind one block number
        if self.current_block_num + 1 != packet.block_num:
            return packets.NoOpPacket()

        block_num = packet.block_num
        self.write_buffer.receive_data(packet.data)
        if len(packet.data) < 512:
            self.state = COMPLETED
            self.log("WRITEREQUEST", "Success")
            self.write_action(self.client_host, self.client_port,
                              self.filename, self.write_buffer.data)
        self.current_block_num += 1
        return packets.AcknowledgementPacket(block_num)
dataset/ETHPy150Open dropbox/emmer/emmer/tftp_conversation.py/TFTPConversation._handle_write_packet/original