Exemple #1
0
def put():
    """
    上传文件
    :return:
    """
    while True:
        user_input = input('put xxx : ').strip()
        if user_input == 'q':
            break
        cmd_list = user_input.split()
        # print(cmd_list)
        if len(cmd_list) < 2:
            print('\033[31;1m命令不合法\033[0m')
            continue
        if cmd_list[0] == 'put':
            if os.path.exists(cmd_list[1]):  # 本地文件存在
                file_name = cmd_list[1].split(os.sep)[-1]
                file_size = os.stat(cmd_list[1]).st_size
                file_md5 = commons.get_file_md5(cmd_list[1])
                print('file:%s, size: %s, md5: %s' %
                      (file_name, file_size, file_md5))
                file_info = {
                    'action': 'put',
                    'filename': file_name,
                    'filesize': file_size,
                    'filemd5': file_md5
                }
                socket_obj.sendall(
                    bytes(json.dumps(file_info),
                          encoding='utf-8'))  # 将文件相关信息发送给服务器
                notify_info = json.loads(
                    socket_obj.recv(1024).decode())  # 接收服务器的通知消息
                current_size = notify_info.get(
                    'current_size')  # 服务端已存在文件大小,不存在为0 --》断点续传
                if notify_info.get('stat') == 'ok':
                    with open(cmd_list[1], 'rb') as file:
                        file.seek(current_size)  # 断点续传
                        for line in file:
                            socket_obj.sendall(line)
                            current_size += len(line)
                            view_bar(current_size, file_size)  # 调用进度条
                    the_rest_size = notify_info.get('chazhi')
                    print('\033[31;1mMission complate ..磁盘剩余空间 %s 字节 \033[0m' %
                          (-the_rest_size))
                    send_success_msg = socket_obj.recv(1024).decode()
                    print(send_success_msg)
                    break
                elif notify_info.get('stat') == 'large':
                    chazhi = notify_info.get('chazhi')
                    print('\033[31;1m文件 %s 太大了。。磁盘空间不足, 多了 %s 字节\033[0m' %
                          (cmd_list[1], chazhi))
                else:
                    print('file exists')
                    break
            else:
                print('\033[31;1m文件 %s 不存在\033[0m' % cmd_list[1])
                continue
        else:
            print('\033[31;1m命令不合法\033[0m')
            continue
 def task_get(self, file_info):
     """
     download file
     :param file_info: about file info
     :return:
     """
     abs_file_path = file_info.get('filename')  # server file abs path
     if not os.path.exists(abs_file_path):  # file not exist
         exist_or_not = {'exist': 'no', 'ip': self.ip.decode()}
         self.request.sendall(bytes(json.dumps(exist_or_not), encoding='utf-8'))
     else:
         file_size = os.stat(abs_file_path).st_size
         file_name = self.ip.decode().strip() + '_' + abs_file_path.split(os.sep)[-1]
         file_md5 = commons.get_file_md5(abs_file_path)
         print(file_name, file_size, file_md5)
         exist_or_not = {'exist': 'yes', 'filename': file_name, 'filesize': file_size, 'filemd5': file_md5}
         self.request.sendall(bytes(json.dumps(exist_or_not), encoding='utf-8'))  # send file info to client
         notify_info = json.loads(self.request.recv(1024).decode())  # receive client notify info
         if notify_info.get('stat') == 'ok':
             with open(abs_file_path, 'rb') as file:
                 file.seek(notify_info.get('current_size'))
                 for line in file:
                     self.request.sendall(line)
             print('\033[31;1mMission complate .. \033[0m')
             send_success_msg = self.request.recv(1024).decode()
             print(send_success_msg)
         else:
             print('\033[31;1mclient file already exists\033[0m')
Exemple #3
0
def file_recv(file_name, file_md5, file_size, current_size, write_method):
    """
    下载文件传输
    :param file_name: 文件名
    :param file_size: 文件大小
    :param current_size: 已存在文件大小
    :param write_method: 读写方式 wb or ab
    :return:
    """
    notify_info = {'current_size': current_size, 'stat': 'ok'}
    socket_obj.sendall(
        bytes(json.dumps(notify_info),
              encoding='utf-8'))  # ask server to start transport file
    with open(file_name, write_method) as new_file:  # 接收写文件
        recv_size = current_size
        while recv_size < file_size:
            data = socket_obj.recv(4096)
            recv_size += len(data)
            new_file.write(data)
            view_bar(recv_size, file_size)
        print('%s receive successful' % file_name)
    local_file_md5 = commons.get_file_md5(file_name)
    print('本地 %s | 服务器 %s' % (local_file_md5, file_md5))
    if local_file_md5 == file_md5:
        msg = '\033[31;1mThe file in server and the local file are just the same\033[0m'
        socket_obj.sendall(bytes(msg, encoding='utf-8'))
        print(msg)
    else:
        msg = '\033[31;1mThe file in server and the local file are different\033[0m'
        socket_obj.sendall(bytes(msg, encoding='utf-8'))
        print(msg)
 def file_recv(self, file_name, file_md5, file_size, current_size, write_method):
     """
     receive file
     :param file_name: file name
     :param file_size: file size
     :param current_size: current file szie
     :param write_method: the method of write file
     :return:
     """
     notify_info = {'current_size': current_size, 'stat': 'ok', 'ip': self.ip.decode()}
     self.request.sendall(bytes(json.dumps(notify_info), encoding='utf-8'))  # ask client to start transport file
     with open(file_name, write_method) as new_file:
         recv_size = current_size
         while recv_size < file_size:
             data = self.request.recv(4096)
             recv_size += len(data)
             new_file.write(data)
         print('%s receive successful' % file_name)
     local_md5 = commons.get_file_md5(file_name)
     print('本地 %s | 客户端 %s' % (local_md5, file_md5))
     if local_md5 == file_md5:
         msg = '\033[31;1mThe file in server and the local file are just the same\033[0m'
         self.request.sendall(bytes(msg, encoding='utf-8'))
         print(msg)
     else:
         msg = '\033[31;1mThe file in server and the local file are different\033[0m'
         self.request.sendall(bytes(msg, encoding='utf-8'))
         print(msg)
Exemple #5
0
def put(ip_port, user_input):
    socket_obj.connect(ip_port)
    while True:
        if user_input == 'q':
            break
        cmd_list = user_input.split()
        if len(cmd_list) < 2:
            print('\033[31;1m命令不合法\033[0m')
            break
        if cmd_list[0] == 'put':
            if os.path.exists(cmd_list[1]):  # 本地文件存在
                file_name = cmd_list[1].split(os.sep)[-1]
                file_size = os.stat(cmd_list[1]).st_size
                file_md5 = commons.get_file_md5(cmd_list[1])
                print('file:%s, size: %s, md5: %s' %
                      (file_name, file_size, file_md5))
                file_info = {
                    'action': 'put',
                    'filename': file_name,
                    'filesize': file_size,
                    'filemd5': file_md5
                }
                socket_obj.sendall(
                    bytes(json.dumps(file_info),
                          encoding='utf-8'))  # 将文件相关信息发送给服务器
                notify_info = json.loads(
                    socket_obj.recv(1024).decode())  # 接收服务器的通知消息
                current_size = notify_info.get(
                    'current_size')  # 服务端已存在文件大小,不存在为0 --》断点续传
                server_ip = notify_info.get('ip').strip()
                if notify_info.get('stat') == 'ok':
                    with open(cmd_list[1], 'rb') as file:
                        file.seek(current_size)  # 断点续传
                        for line in file:
                            socket_obj.sendall(line)
                            current_size += len(line)
                            view_bar(current_size, file_size)  # 调用进度条
                    print('\033[31;1m%s Mission complate ..\033[0m' %
                          server_ip)
                    send_success_msg = socket_obj.recv(1024).decode()
                    print(send_success_msg)
                    break
                else:
                    print('\033[31;1mServer %s file exists\033[0m' % server_ip)
                    break
            else:
                print('\033[31;1m本地文件 %s 不存在\033[0m' % cmd_list[1])
                break
        else:
            print('\033[31;1m命令不合法\033[0m')
            break