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 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 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 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")