def put_body(self): max_length = self.remote_info.max_packet_length file_data = self.file_data # Send the file data. # The optimum size is the maximum packet length accepted by the # remote device minus three bytes for the header ID and length # minus three bytes for the request. optimum_size = max_length - 3 - 3 data = file_data[:optimum_size] self.file_data=self.file_data[optimum_size:] if len(data) == 0: raise Exception("work done") if len(self.file_data) > 0: self.state_put = common.PUT_BODY request = requests.Put() request.add_header(headers.Body(data, False), max_length) self.socket.sendall(request.encode()) else: self.state_put = common.PUT_FINAL request = requests.Put_Final() request.add_header(headers.End_Of_Body(data, False), max_length) self.socket.sendall(request.encode())
def delete(self, name, header_list=()): """delete(self, name, header_list = ()) Requests the deletion of the file with the specified name from the current directory and returns the server's response. """ header_list = [headers.Name(name)] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put_Final() return self._send_headers(request, header_list, max_length)
def _put(self, name, file_data, header_list = ()): header_list = [ headers.Name(name), headers.Length(len(file_data)) ] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put() response = self._send_headers(request, header_list, max_length) yield response if not isinstance(response, responses.Continue): return # Send the file data. # The optimum size is the maximum packet length accepted by the # remote device minus three bytes for the header ID and length # minus three bytes for the request. optimum_size = max_length - 3 - 3 i = 0 while i < len(file_data): data = file_data[i:i+optimum_size] i += len(data) if i < len(file_data): request = requests.Put() request.add_header(headers.Body(data, False), max_length) self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response if not isinstance(response, responses.Continue): return else: request = requests.Put_Final() request.add_header(headers.End_Of_Body(data, False), max_length) self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response if not isinstance(response, responses.Success): return