Exemplo n.º 1
0
 def get(self, conn, args):
     '''
     下载
     :param args:
     :return:
     '''
     if not args['msg'].index(args['msg'][-1]):  # 判断是否只输入了一个命令
         user_home_path = os.path.join(settings.home_path, self.username)
         msg_dic = {'msg': ['dir', user_home_path]}
         self.all_func(conn, msg_dic)  # 返回用户家目录
     else:  # 否则开始下载文件
         filename = args['msg'][1]
         file_path = os.path.join(settings.home_path, self.username,
                                  filename)
         filesize = os.path.getsize(file_path)
         head_dic = {'filename': filename, 'filesize': filesize}
         common.Head(conn, head_dic).pack()
         send_size = 0
         with open(file_path, 'rb') as f:
             while True:
                 data = f.read(self.packet_size)
                 if not data: break
                 conn.send(data)
                 send_size += len(data)
                 common.progress_bar(send_size, filesize)
         common.Head(conn, {'msg': True}).pack()
Exemplo n.º 2
0
 def get(self, *args):
     '''
     下载,查看家目录
     :param args:
     :return:
     '''
     if not args[0].index(args[0][-1]):  # 查看家目录
         self.all_func(args[0])  # 传入命令参数,调用执行系统命令的函数
     else:  # 下载
         head_dic = {'cmd': args[0][0], 'msg': args[0]}
         common.Head(self.socket, head_dic).pack()  # 告诉服务端,客户端要下载文件以及文件名
         info = common.Head.unpack(self.socket)
         filesize = info['filesize']
         file_path = os.path.join(settings.home_path, self.username,
                                  info['filename'])
         recv_size = 0
         with open(file_path, 'wb') as f:
             while recv_size < filesize:
                 if filesize - recv_size < self.max_size:
                     self.max_size = filesize - recv_size
                 recv_data = self.socket.recv(self.max_size)
                 f.write(recv_data)
                 recv_size += len(recv_data)
                 common.progress_bar(recv_size, filesize)
         if common.Head.unpack(self.socket)['msg']:
             print('下载成功!!!')
Exemplo n.º 3
0
    def put(self, args):
        '''
        上传
        :param args: 分割的命令列表
        :return:
        '''
        filesize = args['filesize']
        file_path = os.path.join(settings.home_path, self.username,
                                 args['filename'])
        disk_size = os.path.getsize(os.path.dirname(file_path))

        if filesize > self.home_size or filesize > self.home_size - disk_size:  # 判断磁盘额度是否够用
            head_dic = {'message': False}
            common.Head(self.conn, head_dic).pack()
            return
        else:
            head_dic = {'message': True}
            common.Head(self.conn, head_dic).pack()

        if os.path.exists(file_path):  # 判断文件是否存在
            file_path_size = os.path.getsize(file_path)
            if file_path_size < filesize:  # 是否需要续传
                common.Head(self.conn, {'message': file_path_size}).pack()
                self.re_put(file_path, file_path_size, filesize)
                return
            else:
                common.Head(self.conn, {'message': '文件已存在'}).pack()
                return
        else:
            common.Head(self.conn, {'message': False}).pack()

        recv_size = 0
        hasa_value = 0
        with open(file_path, 'wb') as f:
            while recv_size < filesize:
                if filesize - recv_size < self.packet_size:  # 防止粘包
                    self.packet_size = filesize - recv_size
                recv_data = self.conn.recv(self.packet_size)
                f.write(recv_data)
                hasa_value = common.hs(recv_data)  # 哈希模块
                hasa_value += hasa_value
                recv_size += len(recv_data)
                common.progress_bar(recv_size, filesize)  # 调用进度条模块

        hs_value = common.Head.unpack(self.conn)
        if hs_value['hs_value'] == common.hs(hasa_value):  # 哈希校验文件
            common.Head(self.conn, {'message': True}).pack()
        else:
            common.Head(self.conn, {'message': False}).pack()
Exemplo n.º 4
0
 def re_put(self, conn, file_path, recv_size, filesize):
     '''
     断点续传
     :param file_path: 文件路径
     :param filesize: 文件大小
     :return:
     '''
     with open(file_path, 'ab') as f:
         while recv_size < filesize:
             if filesize - recv_size < self.packet_size:  # 防止粘包
                 self.packet_size = filesize - recv_size
             recv_data = conn.recv(self.packet_size)
             f.write(recv_data)
             recv_size += len(recv_data)
             common.progress_bar(recv_size, filesize)
Exemplo n.º 5
0
 def re_put(self, file_path_size, filesize, filename):
     '''
     断点续传
     :param file_path_size: 已上传的文件大小
     :param filesize: 文件总大小
     :param filename: 文件路径
     :return:
     '''
     with open(filename, 'rb') as f:
         while True:
             f.seek(file_path_size)
             data = f.read(self.max_size)
             if not data: break
             self.socket.send(data)
             file_path_size += len(data)
             common.progress_bar(file_path_size, filesize)
     print('上传成功')
Exemplo n.º 6
0
    def put(self, args):
        '''
        上传
        :param args: 分割的命令列表
        :return:
        '''
        filesize = os.path.getsize(args[1])
        head_dic = {
            'cmd': args[0],
            'filename': args[1],
            'filesize': filesize
        }
        common.Head(self.socket, head_dic).pack()
        info = common.Head.unpack(self.socket)
        if not info['message']:
            print('磁盘额度不够')
            return

        msg = common.Head.unpack(self.socket)['message']
        if msg or msg == 0:                  # 文件是否存在
            if msg == '文件已存在':          # 判断是否需要续传
                print('文件已存在')
                return
            else:
                print('正在续传!!!')
                self.re_put(msg, filesize, args[1])
                return

        send_size = 0
        hasa_value = 0
        with open(args[1], 'rb') as f:
            while True:
                line = f.read(self.max_size)
                if not line: break
                self.socket.send(line)
                hasa_value = common.hs(line)
                hasa_value += hasa_value
                send_size += len(line)
                common.progress_bar(send_size, filesize)
        hs_value = common.hs(hasa_value)                # 文件哈希值
        common.Head(self.socket, {'hs_value': hs_value}).pack()
        if common.Head.unpack(self.socket)['message']:
            print('文件上传成功!!!')