def download_file(login: str, password: str, file_name: str, path: str): with Network(APIServerIp, APIServerPort) as network: is_path_set = set_path(login, password, path, network) total_file_size = 0 data = [] offset = 0 if is_path_set is not None and is_path_set == Responses.OK_RESPONSE.value: while True: request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.FILES_TYPE, FilesRequests.DOWNLOAD_FILE). \ set_header("File-Name", file_name). \ set_header("Range", offset) \ .build() request = HTTPBuilder.insert_size_header_to_http_message( request) network.send(request) response = HTTPParser(network.receive()) total_file_size = response.get_header("Total-File-Size") data.append(response.get_body()) offset += len(data[-1]) if total_file_size is not None: break return b''.join(data) return "Не удалось скачать файл"
def set_path(login: str, password: str, path: str, network: Network = None): is_network_not_passed = network is None if is_network_not_passed: network = Network(APIServerIp, APIServerPort) response = None is_authorized, error_message = authorization(login, password, network) if is_authorized: body = "folder=" + path request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.CONTROL_TYPE, ControlRequests.SET_PATH). \ set_header("Content-Length", len(body)). \ build(body.encode("CP1251")) request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = Responses.OK_RESPONSE.value if HTTPParser( network.receive()).get_body().decode() == Responses.OK_RESPONSE.value else None if is_network_not_passed: network.close() return response
def registration(login: str, password: str): with Network(APIServerIp, APIServerPort) as network: body = "login={}&password={}".format(login, password) request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.ACCOUNT_TYPE, AccountRequests.REGISTRATION). \ set_header("Content-Length", str(len(body))). \ build(body.encode("CP1251")) request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = HTTPParser(network.receive()) return response.get_header("Error") == "0", response.get_body().decode( "CP1251")
def upload_file(login: str, password: str, file_name: str, file_data: bytes, path: str): with Network(APIServerIp, APIServerPort) as network: is_path_set = set_path(login, password, path, network) if is_path_set is not None and is_path_set == Responses.OK_RESPONSE.value: offset = 0 file_data_size = len(file_data) data = bytes() is_last = False while True: if file_data_size - offset >= FILE_PACKET_SIZE: data = file_data[offset:FILE_PACKET_SIZE] else: data = file_data[offset:] is_last = True message = HTTPBuilder().set_method("POST"). \ set_header(RequestType.FILES_TYPE, FilesRequests.UPLOAD_FILE). \ set_header("File-Name", file_name). \ set_header("Range", offset). \ set_header("Content-Length", len(data)). \ set_header("Total-File-Size" if is_last else "Reserved", file_data_size if is_last else 0). \ build(data) message = HTTPBuilder.insert_size_header_to_http_message( message) network.send(message) offset += len(data) if is_last: break response = HTTPParser(network.receive()) return response.get_header( "Error") == "0", response.get_body().decode("CP1251") return False, "Не удалось загрузить файл"
def get_files(login: str, password: str, path: str): with Network(APIServerIp, APIServerPort) as network: data = None is_path_set = set_path(login, password, path, network) if is_path_set is not None and is_path_set == Responses.OK_RESPONSE.value: request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.FILES_TYPE, FilesRequests.SHOW_ALL_FILES_IN_DIRECTORY). \ build() request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = HTTPParser(network.receive()) if response.get_header("Error") == "1": return response.get_body().decode("CP1251") body = response.get_body().decode("CP1251") data = [] tem = [[] for _ in range(6)] cur_index = 0 for i in body: if i == DATA_DELIMITER[0]: data.append( FileData(''.join(tem[0]), ''.join(tem[1]), ''.join(tem[2]), ''.join(tem[3]), ''.join(tem[4]), int(''.join(tem[5])))) cur_index = 0 tem = [[] for _ in range(6)] elif i == DATA_PART_DELIMITER[0]: cur_index += 1 else: tem[cur_index].append(i) return data
def remove_file(login: str, password: str, path: str, file_name: str): with Network(APIServerIp, APIServerPort) as network: is_path_set = set_path(login, password, path, network) if is_path_set is not None and is_path_set == Responses.OK_RESPONSE.value: request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.FILES_TYPE, FilesRequests.REMOVE_FILE). \ set_header("File-Name", file_name). \ build() request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = HTTPParser(network.receive()) if response.get_header("Error") == "1": return "Не удалось удалить файл" else: return "Файл успешно удален" return "Не удалось удалить файл"
def create_folder(login: str, password: str, folder_name: str, path: str): with Network(APIServerIp, APIServerPort) as network: is_path_set = set_path(login, password, path, network) if is_path_set is not None and is_path_set == Responses.OK_RESPONSE.value: body = "folder=" + folder_name request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.FILES_TYPE, FilesRequests.CREATE_FOLDER). \ set_header("Content-Length", len(body)). \ build(body.encode("CP1251")) request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = HTTPParser(network.receive()) return "Папка успешно создана" if response.get_header( "Error") == "0" else "Не удалось создать папку" return "Не удалось создать папку"
def authorization(login: str, password: str, network: Network = None): is_network_not_passed = network is None if is_network_not_passed: network = Network(APIServerIp, APIServerPort) body = "login={}&password={}".format(login, password) request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.ACCOUNT_TYPE, AccountRequests.AUTHORIZATION). \ set_header("Content-Length", str(len(body))). \ build(body.encode("CP1251")) request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = HTTPParser(network.receive()) if is_network_not_passed: network.close() return response.get_header("Error") == "0", response.get_body().decode("CP1251")
def receive(self): data = self._socket.recv(HTTP_PACKET_SIZE) size = 0 last_packet = len(data) while True: if last_packet == 0: raise RuntimeError("socket connection broken") if len(data) > 25 and size == 0: parser = HTTPParser(data) size = int(parser.get_header("Total-HTTP-Message-Size")) if size == len(data): break data += self._socket.recv(size - len(data)) last_packet = len(data) - last_packet return data