def setpath(self, name="", create_dir=False, to_parent=False, header_list=()): """setpath(self, name = "", create_dir = False, to_parent = False, header_list = ()) Requests a change to the server's current directory for the session to the directory with the specified name, and returns the response. This method is also used to perform other actions, such as navigating to the parent directory (set to_parent to True) and creating a new directory (set create_dir to True). Additional headers can be sent by passing a sequence as the header_list keyword argument. These will be sent after the name information. """ header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length flags = 0 if not create_dir: flags |= requests.Set_Path.DontCreateDir if to_parent: flags |= requests.Set_Path.NavigateToParent request = requests.Set_Path((flags, 0)) response = self._send_headers(request, header_list, max_length) return response
def _get(self, name=None, header_list=()): header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length request = requests.Get() response = self._send_headers(request, header_list, max_length) yield response if not isinstance(response, responses.Continue) and \ not isinstance(response, responses.Success): return # Retrieve the file data. file_data = [] request = requests.Get_Final() while isinstance(response, responses.Continue): self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response
def _handle_get(self, sock, mime=None, name=None): body = None response = responses.Not_Found() header_list = [] if callable(self.get_listener): body = self.get_listener(mime=mime, name=name, conn=sock) print body if hasattr(self, "remote_info"): max_length = self.remote_info.max_packet_length else: max_length = self.max_packet_length if body: response = responses.Success() if not name: name = "vCard.vcf" response.add_header(headers.Name(name), max_length) response.add_header(headers.Length(len(body)), max_length) response.add_header(headers.End_Of_Body(body), max_length) #header_list.append(headers.Length(len(body))) #header_list.append(headers.End_Of_Body(body)) self._send_headers(sock, response, header_list, max_length)
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)), headers.End_Of_Body(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
def mobex_cmd(self, typ, name=None, extra_headers=()): header_list = [ headers.Type(typ), ] if name is not None: header_list.append(headers.Name(name)) header_list += extra_headers max_length = self.remote_info.max_packet_length request = requests.Put_Final() return self._send_headers(request, header_list, max_length)
def get(self, name = None, header_list = ()): """get(self, name = None, header_list = ()) Requests the specified file from the server's current directory for the session. If successful, returns a tuple containing a list of responses received during the operation and the file data received. If unsuccessful, a single response object is returned. Additional headers can be sent by passing a sequence as the header_list keyword argument. These will be sent after the name information. """ header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length request = requests.Get() response = self._send_headers(request, header_list, max_length) if not isinstance(response, responses.Continue) and \ not isinstance(response, responses.Success): return response # Retrieve the file data. returned_headers = [] file_data = [] request = requests.Get_Final() while isinstance(response, responses.Continue): returned_headers += response.header_data self.con.send(request.encode()) response = self.response_handler.decode(self.con) if not isinstance(response, responses.Success): return response returned_headers += response.header_data return self._collect_parts(returned_headers)
def put(self, name, file_data, header_list = ()): """put(self, name, file_data, header_list = ()) Sends a file with the given name, containing the file_data specified, to the server for storage in the current directory for the session, and returns the response. Additional headers can be sent by passing a sequence as the header_list keyword argument. These will be sent after the name and file length information associated with the name and file_data supplied. """ 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) if not isinstance(response, responses.Continue): return response # 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.con.send(request.encode()) response = self.response_handler.decode(self.con) if not isinstance(response, responses.Continue): return response else: request = requests.Put_Final() request.add_header(headers.End_Of_Body(data, False), max_length) self.con.send(request.encode()) response = self.response_handler.decode(self.con) if not isinstance(response, responses.Success): return response return response