Exemplo n.º 1
0
    def logon(self, *args):
        """服务器端,注册接口"""
        cmd_dct = args[0]
        user_path = os.path.join(users_path, cmd_dct["name"] + ".json")
        print(user_path)

        # 验证用户是否存在
        if os.path.exists(user_path):
            print("此用户已经存在")
            res = {"status": "-1"}
            self.request.send(json.dumps(res).encode("utf-8"))
        else:
            users.add_user(cmd_dct['name'], cmd_dct['password'])
            user = json.load(open(user_path, "r"))
            # 准备用户目录
            self.current = user["name"]
            self.username = user["name"]
            current_path = os.path.join(home, self.current)
            if not os.path.exists(current_path):
                os.mkdir(current_path)

            userinfo = users.getinfo(self.username)
            res = {
                "status": "0",
                "total_size": userinfo["total_size"],
                "used_size": userinfo["total_size"] - userinfo["used_size"]
            }
            self.request.send(json.dumps(res).encode("utf-8"))
            print("注册成功")
            return  # 注册成功返回
Exemplo n.º 2
0
    def put(self, *args):
        """服务器端,接收文件"""
        cmd_dct = args[0]
        current_path = os.path.join(home, self.current)
        filename = os.path.join(current_path, cmd_dct["filename"])
        filesize = cmd_dct["size"]

        # 验证用户空间是否足够 确认接收
        userinfo = users.getinfo(self.username)
        if userinfo["total_size"] - userinfo["used_size"] > filesize:
            self.request.send("0".encode("utf-8"))
            print("空间足够")
        else:
            self.request.send("-1".encode("utf-8"))
            print("空间不足")
            return

        # 接收文件内容
        if os.path.isfile(filename):
            f = open("new_" + filename, "wb")
        else:
            f = open(filename, "wb")
        received_size = 0
        m = hashlib.md5()
        while received_size < filesize:
            # 精确控制接收数据大小
            if filesize - received_size > 1024:
                size = 1024
            else:
                size = filesize - received_size
            data = self.request.recv(size)

            f.write(data)
            m.update(data)
            received_size += len(data)
        else:
            f.close()
            # 增加已经使用的空间
            users.add_used_size(self.username, received_size)

            # MD5值校验
            received_md5 = self.request.recv(1024).decode("utf-8")
            if m.hexdigest() == received_md5:
                print("文件上传成功")
                self.request.send("0".encode("utf-8"))
            else:
                print("文件上传出错")
                self.request.send("-1".encode("utf-8"))
Exemplo n.º 3
0
    def put(self, *args):
        '''
        Receive files
        :param args:
        :return:
        '''
        cmd_dct = args[0]
        current_path = os.path.join(home, self.current)
        filename = os.path.join(current_path, cmd_dct['filename'])
        filesize = cmd_dct['size']

        userinfo = users.getinfo(self.username)
        if userinfo['total_size'] - userinfo['used_size'] > filesize:
            self.request.send('0'.encode('UTF-8'))
            print('Enough to store.'.center(50, ' '))
        else:
            self.request.send("-1".encode('UTF-8'))
            print('No enough space to store'.center(50, ' '))
            return

        if os.path.isfile(filename):
            f = open('new_' + filename, 'wb')
        else:
            f = open(filename, 'wb')
        received_size = 0
        m = hashlib.md5()
        while received_size < filesize:
            if filesize - received_size > 1024:
                size = 1024
            else:
                size = filesize - received_size
            data = self.request.recv(size)

            f.write(data)
            m.update(data)
            received_size += len(data)
        else:
            f.close()
            users.add_used_size(self.username, received_size)

            # Check MD5
            received_md5 = self.request.recv(1024).decode('UTF-8')
            if m.hexdigest() == received_md5:
                print('Upload Successfully.')
                self.request.send("0".encode('UTF-8'))
            else:
                print('Upload failed.')
                self.request.send("-1".encode('UTF-8'))
Exemplo n.º 4
0
    def login(self, *args):
        """服务器端,登陆接口"""
        cmd_dct = args[0]
        user_path = os.path.join(users_path, cmd_dct["name"] + ".json")
        print(user_path)

        # 验证用户是否存在
        if os.path.isfile(user_path):
            user = json.load(open(user_path, "r"))

            if cmd_dct["password"] == user["password"]:

                # 准备用户目录
                self.current = user["name"]
                self.username = user["name"]
                current_path = os.path.join(home, self.current)
                if not os.path.exists(current_path):
                    os.mkdir(current_path)

                userinfo = users.getinfo(self.username)

                res = {
                    "status": "0",
                    "total_size": userinfo["total_size"],
                    "used_size": userinfo["total_size"] - userinfo["used_size"]
                }
                self.request.send(json.dumps(res).encode("utf-8"))
                print("登陆成功")

                return  # 登陆成功返回
            else:
                print("密码错误")
        else:
            print("用户不存在")

        res = {"status": "-1"}
        self.request.send(json.dumps(res).encode("utf-8"))