示例#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()
示例#2
0
 def login(self, conn, info):
     self.username = info['user'][0]
     password = info['user'][1]
     conf = configparser.ConfigParser()
     conf.read(settings.user_path)
     for dic in conf.sections():
         if self.username == dic and password == conf[dic][
                 'password']:  # 验证用户名及密码
             self.home_size = eval(conf[dic]['disk_limit'])
             head_dic = {'message': True}
             common.Head(conn, head_dic).pack()  # 返回验证信息
             return
     else:
         head_dic = {'message': False}
         common.Head(conn, head_dic).pack()
示例#3
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('下载成功!!!')
示例#4
0
 def cd(self, conn, args):
     '''
     切换目录
     :param args:
     :return:
     '''
     info = args['msg']
     common.Head(conn, {'msg': '已切换到%s目录,使用dir命令查看' % info[1]}).pack()
     self.cd_path = info[1]  # 记录切换的目录
示例#5
0
 def cd(self, args):
     '''
     切换目录
     :param args:
     :return:
     '''
     head_dic = {'cmd': args[0], 'msg': args}
     common.Head(self.socket, head_dic).pack()
     info = common.Head.unpack(self.socket)['msg']
     print(info)
示例#6
0
文件: client.py 项目: yang91797/FTP
    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('文件上传成功!!!')
示例#7
0
文件: server.py 项目: yang91797/FTP
    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()
示例#8
0
    def all_func(self, args):
        '''
        查看目录文件,执行系统命令
        :param args:
        :return:
        '''
        head_dic = {'cmd': args[0], 'msg': args}
        common.Head(self.socket, head_dic).pack()
        res = common.Head.unpack(self.socket)['data_size']
        recv_size = 0
        recv_data = b''
        while recv_size < res:
            if res - recv_size < self.max_size:
                self.max_size = res - recv_size
            data = self.socket.recv(self.max_size)
            recv_size += len(data)
            recv_data += data

        print(recv_data.decode(self.coding_dir))
示例#9
0
 def all_func(self, conn, args):
     '''
     查看目录文件
     :param args:
     :return:
     '''
     cmd = args['msg']
     if self.cd_path and args['msg'][-1] == 'dir':  # 切换目录后,使用dir命令查看
         cmd = ['dir', self.cd_path]
     res = subprocess.Popen(cmd,
                            shell=True,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
     out_res = res.stdout.read()
     err_res = res.stderr.read()
     data_size = len(out_res) + len(err_res)
     head_dic = {'data_size': data_size}
     common.Head(conn, head_dic).pack()
     conn.send(out_res)
     conn.send(err_res)
示例#10
0
文件: client.py 项目: yang91797/FTP
 def login(self):
     '''
     登陆
     :return:
     '''
     while True:
         username = input('请输入用户名:').strip()
         password = input('请输入密码:').strip()
         password = common.hs(password)          # 哈希模块
         user = [username, password]
         head_dic = {'cmd': 'login',
                     'user': user,
                     }
         for key in head_dic:
             if not all(head_dic[key]): continue
         common.Head(self.socket, head_dic).pack()               # 发送报头模块
         re_head = common.Head.unpack(self.socket)               # 接收报头模块
         if re_head['message']:
             print('登陆成功')
             self.username = username            # 记录用户名
             self.run()
         else:
             print('用户名或密码错误!!!')