Beispiel #1
0
    def put(self, conn):
        """从client上传文件到server当前工作目录下
        """
        if len(self.cmds) > 1:
            obj = conn.recv(4)
            state_size = struct.unpack('i', obj)[0]
            if state_size:
                # 算出了home下已被占用的大小self.home_bytes_size
                self.current_home_size()
                header_bytes = conn.recv(struct.unpack('i', conn.recv(4))[0])
                header_dic = pickle.loads(header_bytes)
                filename = header_dic.get('filename')
                file_size = header_dic.get('file_size')
                file_md5 = header_dic.get('file_md5')
                self.file_path = os.path.join(os.getcwd(), filename)
                if os.path.exists(self.file_path):
                    conn.send(struct.pack('i', 1))
                    has_size = os.path.getsize(self.file_path)
                    if has_size == file_size:
                        print("\033[31;1mfile already does exist!\033[0m")
                        conn.send(struct.pack('i', 0))
                    else:
                        print(
                            '\033[31;1mLast time  file not finished,this time continue\033[0m')
                        conn.send(struct.pack('i', 1))
                        if self.home_bytes_size + int(file_size - has_size) > self.quota_bytes:
                            print(
                                '\033[31;1mSorry exceeding user quotas\033[0m')
                            conn.send(struct.pack('i', 0))
                        else:
                            conn.send(struct.pack('i', 1))
                            conn.send(struct.pack('i', has_size))
                            with open(self.file_path, 'ab') as f:
                                f.seek(has_size)
                                File_func().write_file(conn, f, has_size, file_size)
                            Md5_func().verification_filemd5(self.file_path, conn, file_md5)

                else:
                    conn.send(struct.pack('i', 0))
                    print('\033[31;1mfile does not exist, now first put\033[0m')
                    if self.home_bytes_size + int(file_size) > self.quota_bytes:
                        print('\033[31;1mSorry exceeding user quotas\033[0m')
                        conn.send(struct.pack('i', 0))
                    else:
                        conn.send(struct.pack('i', 1))
                        with open(self.file_path, 'wb') as f:
                            recv_size = 0
                            File_func().write_file(conn, f, recv_size, file_size)
                        Md5_func().verification_filemd5(self.file_path, conn, file_md5)

            else:
                print("\033[31;1mfile does not exist!\033[0m")
        else:
            print("\033[31;1muser does not enter file name\033[0m")
Beispiel #2
0
 def put(self, cmds):
     """往server端登录的用户目录下上传文件
     """
     if len(cmds) > 1:
         filename = cmds[1]
         file_path = os.path.join(settings.upload_filepath, filename)
         if os.path.isfile(file_path):  # 如果文件存在,支持断电续传
             self.socket.send(struct.pack('i', 1))
             self.file_path = file_path
             file_size = os.path.getsize(self.file_path)
             header_dic = {
                 'filename': os.path.basename(filename),
                 'file_md5': Md5_func().getfile_md5(self.file_path),
                 'file_size': file_size
             }
             header_bytes = pickle.dumps(header_dic)
             self.socket.send(struct.pack('i', len(header_bytes)))
             self.socket.send(header_bytes)
             state = struct.unpack('i', self.socket.recv(4))[0]
             if state:  # 已经存在
                 has_state = struct.unpack('i', self.socket.recv(4))[0]
                 if has_state:
                     self.put_situation(file_size, 1)
                 else:  # 存在的大小 和文件大小一致 不必再传
                     print("\033[31;1mfile already does exist!\033[0m")
             else:  # 第一次传
                 self.put_situation(file_size)
         else:  # 文件不存在
             print("\033[31;1mfile does not exist!\033[0m")
             self.socket.send(struct.pack('i', 0))
     else:
         print("\033[31;1muser does not enter file name\033[0m")
Beispiel #3
0
 def get(self, cmds):
     """从server下载文件到client
     """
     if len(cmds) > 1:
         filename = cmds[1]
         self.file_path = os.path.join(settings.down_filepath, filename)
         if os.path.isfile(self.file_path):  # 如果文件存在,支持断电续传
             temp_file_size = os.path.getsize(self.file_path)
             self.socket.send(struct.pack('i', temp_file_size))
             header_size = struct.unpack('i', self.socket.recv(4))[0]
             if header_size:
                 filename, file_size, filemd5 = File_func(
                 ).recv_file_header(self.socket, header_size)
                 if temp_file_size == file_size:
                     print('\033[34;1mFile already does exist\033[0m')
                 else:
                     print(
                         '\033[34;1mFile now is breakpoint continuation\033[0m'
                     )
                     File_func().appendfile_content(self.socket,
                                                    self.file_path,
                                                    temp_file_size,
                                                    file_size)
                     Md5_func().verification_filemd5(
                         self.file_path, filemd5)
             else:
                 print(
                     "\033[34;1mFile was downloaded before,but now server's file is not exist\033[0m"
                 )
         else:  # 如果文件不存在,则是直接下载
             self.socket.send(struct.pack('i', 0))
             obj = self.socket.recv(1024)
             header_size = struct.unpack('i', obj)[0]
             if header_size == 0:
                 print("\033[31;1mfile does not exist!\033[0m")
             else:
                 filename, file_size, filemd5 = File_func(
                 ).recv_file_header(self.socket, header_size)
                 download_filepath = os.path.join(settings.down_filepath,
                                                  filename)
                 with open(download_filepath, 'wb') as f:
                     get_size = 0
                     File_func().write_file(self.socket, f, get_size,
                                            file_size)
                 Md5_func().verification_filemd5(self.file_path, filemd5)
     else:
         print("\033[31;1muser does not enter file name\033[0m")
Beispiel #4
0
    def get(self, conn):
        '''
       下载,首先查看文件是否存在,然后上传文件的报头大小,上传文件,以读的方式发开文件
       找到下载的文件
        发送 header_size
        发送 header_bytes file_size
        读文件 rb 发送 send(line)
        若文件不存在,发送0 client提示:文件不存在
       :param cmds:
       :return:
               '''
        if len(self.cmds) > 1:
            filename = self.cmds[1]
            self.file_path = os.path.join(os.getcwd(), filename)
            if os.path.isfile(self.file_path):
                file_size = os.path.getsize(self.file_path)
                obj = conn.recv(4)
                exist_file_size = struct.unpack('i', obj)[0]
                header = {
                    'filename': filename,
                    'filemd5': Md5_func().getfile_md5(self.file_path),
                    'file_size': file_size
                }
                header_bytes = pickle.dumps(header)
                conn.send(struct.pack('i', len(header_bytes)))
                conn.send(header_bytes)
                if exist_file_size:  # 表示之前被下载过 一部分
                    if exist_file_size != file_size:
                        File_func().send_filedata(self.file_path,
                                                  exist_file_size)
                    else:
                        print(
                            '\033[31;1mbreakpoint and file size are the same\033[0m'
                        )
                else:  # 文件第一次下载
                    File_func().send_filedata(self.file_path, conn)
            else:
                print('\033[31;1merror\033[0m')
                conn.send(struct.pack('i', 0))

        else:
            print("\033[31;1muser does not enter file name\033[0m")