def create_user(self): """ 创建一个新用户 :return: """ try: kwargs = dict(password=self.password, islocked=str(self.islocked), isdel=str(self.isdel), totalspace=str(self.totalspace), usedspace=str(self.usedspace)) dbapi.add_option(self.username, **kwargs) self.__create_folder() except Exception as e: common.writelog(e, "error")
def show(client_socket, client_user, recv_data): """ 执行用户的show命令,将当前文件夹(用户对象的当前路径属性 self.currpath)下的文件显示出来 :param client_socket: 客户端socket对象 :param client_user: 客户端 用户 对象 :param recv_data: 用户发送过来的数据 show| :return: 返回用户对象的 self.currpath 目录下的所有文件及文件夹串 """ _check_folder = client_user.currpath # 获取所有文件名 或 文件夹名的 列表 file_list = os.listdir(_check_folder) # 目录下的文件数 file_count = len(file_list) # 如果有文件 if file_count > 0: return_list = "{filecount}|".format(filecount=file_count) for i in file_list: f = os.path.join(_check_folder, i) stat = os.stat(f) create_time = time.strftime('%Y:%m-%d %X', time.localtime(stat.st_mtime)) file_size = stat.st_size if os.path.isfile(f): return_list += "{ctime} {fsize} {fname}\n".format( ctime=create_time, fsize=str(file_size).rjust(10, " "), fname=i) if os.path.isdir(f): return_list += "{ctime} <DIR> {fsize} {fname}\n".format( ctime=create_time, fsize=str(file_size).rjust(10, " "), fname=i) else: return_list = "0|" try: # 开始发送信息到客户端 # 1 先把结果串的大小发过去 str_len = len(return_list.encode("utf-8")) client_socket.send(bytes(str(str_len), encoding='utf8')) # 2 接收客户端 read 标识,防止连包 read_stat = client_socket.recv(100).decode() if read_stat == "ready": client_socket.sendall(return_list.encode('utf-8')) # client_socket.sendall(bytes(return_list, encoding='utf8')) else: common.writelog( "client send show command,send 'ready' status fail", "info") except Exception as e: common.writelog(e, "error")
def show(client_socket, client_user, recv_data): """ 执行用户的show命令,将当前文件夹(用户对象的当前路径属性 self.currpath)下的文件显示出来 :param client_socket: 客户端socket对象 :param client_user: 客户端 用户 对象 :param recv_data: 用户发送过来的数据 show| :return: 返回用户对象的 self.currpath 目录下的所有文件及文件夹串 """ _check_folder = client_user.currpath # 获取所有文件名 或 文件夹名的 列表 file_list = os.listdir(_check_folder) # 目录下的文件数 file_count = len(file_list) # 如果有文件 if file_count > 0: return_list = "{filecount}|".format(filecount=file_count) for i in file_list: f = os.path.join(_check_folder, i) stat = os.stat(f) create_time = time.strftime('%Y:%m-%d %X', time.localtime(stat.st_mtime)) file_size = stat.st_size if os.path.isfile(f): return_list += "{ctime} {fsize} {fname}\n".format(ctime=create_time, fsize=str(file_size).rjust(10, " "), fname=i) if os.path.isdir(f): return_list += "{ctime} <DIR> {fsize} {fname}\n".format(ctime=create_time, fsize=str(file_size).rjust(10, " "), fname=i) else: return_list = "0|" try: # 开始发送信息到客户端 # 1 先把结果串的大小发过去 str_len = len(return_list.encode("utf-8")) client_socket.send(bytes(str(str_len), encoding='utf8')) # 2 接收客户端 read 标识,防止连包 read_stat = client_socket.recv(100).decode() if read_stat == "ready": client_socket.sendall(return_list.encode('utf-8')) # client_socket.sendall(bytes(return_list, encoding='utf8')) else: common.writelog("client send show command,send 'ready' status fail", "info") except Exception as e: common.writelog(e, "error")
def cd(client_socket, client_user, recv_data): """ 对用户的cd命令进行操作,如果合法则修改用户对象self.currpath 为指定路径 结果状态:0: 已经是家目录,1: 进入成功 2: 传入的文件夹名不是文件夹 如果是 ..: 表示返回上一级菜单,如果当前目录已经是家目录则返回结果状态0,否则返回 1 如果是非..: 如果 目录名为非目录,返回2, 否则返回1 :param client_socket: 客户端socket对象 :param client_user: 客户端用户对象 :param recv_data: 接收的命令 "cd|[folder]" -> [folder]= .. or foldername :return: {结果状态(0,1,2)|目录名} """ # 获取用户进入的目录 cd_folder = recv_data.split("|")[1] # 返回上一级? try: if cd_folder == "..": # 如果当前已经是家目录了 if client_user.currpath == client_user.homepath: send_data = "0|{0}".format( os.path.basename(client_user.currpath)) else: client_user.currpath = os.path.dirname(client_user.currpath) send_data = "1|{0}".format( os.path.basename(client_user.currpath)) else: # 组合路径 tmp_path = os.path.join(client_user.currpath, cd_folder) # 是文件夹吗? if os.path.isdir(tmp_path): client_user.currpath = tmp_path send_data = "1|{0}".format( os.path.basename(client_user.currpath)) else: # 不是文件夹 send_data = "2|{0}".format(cd_folder) # 开始发送结果 client_socket.sendall(bytes(send_data, 'utf8')) except Exception as e: common.writelog(e, "error")
def cd(client_socket, client_user, recv_data): """ 对用户的cd命令进行操作,如果合法则修改用户对象self.currpath 为指定路径 结果状态:0: 已经是家目录,1: 进入成功 2: 传入的文件夹名不是文件夹 如果是 ..: 表示返回上一级菜单,如果当前目录已经是家目录则返回结果状态0,否则返回 1 如果是非..: 如果 目录名为非目录,返回2, 否则返回1 :param client_socket: 客户端socket对象 :param client_user: 客户端用户对象 :param recv_data: 接收的命令 "cd|[folder]" -> [folder]= .. or foldername :return: {结果状态(0,1,2)|目录名} """ # 获取用户进入的目录 cd_folder = recv_data.split("|")[1] # 返回上一级? try: if cd_folder == "..": # 如果当前已经是家目录了 if client_user.currpath == client_user.homepath: send_data = "0|{0}".format(os.path.basename(client_user.currpath)) else: client_user.currpath = os.path.dirname(client_user.currpath) send_data = "1|{0}".format(os.path.basename(client_user.currpath)) else: # 组合路径 tmp_path = os.path.join(client_user.currpath, cd_folder) # 是文件夹吗? if os.path.isdir(tmp_path): client_user.currpath = tmp_path send_data = "1|{0}".format(os.path.basename(client_user.currpath)) else: # 不是文件夹 send_data = "2|{0}".format(cd_folder) # 开始发送结果 client_socket.sendall(bytes(send_data, 'utf8')) except Exception as e: common.writelog(e, "error")
tmpflag = False while not tmpflag: username = common.input_msg("输入用户名[q返回]: ") if username == "q": tmpflag = True continue # 创建用户对象 new_user = Users(username) if not new_user.exists: userpasswd = common.input_msg("设置初始密码[默认12345]: ", default="12345") totalspace = common.input_msg("设置磁盘配额[默认500M]: ", default=str(settings.HOME_QUOTA)) print("\n 正在初始化用户,请稍等.........\n") new_user.password = common.encry_sha(userpasswd) new_user.islocked = 0 new_user.isdel = 0 new_user.totalspace = int(totalspace) * 1024 * 1024 new_user.usedspace = 0 new_user.create_user() print("初始化成功!") else: print("\033[1;30m用户已经存在!\033[0m;\n") continue except Exception as e: common.writelog("start - main - type2 - {0}".format(e), "error")
def put(client_socket, client_user, recv_data): """ 用户上传文件,服务端接收模块,先发送一个准备接收状态 如果不是断点续传文件,直接发送"4000|recved_size(0)" :param client_socket: 客户端socket对象 :param client_user: 客户端用户对象 :param recv_data: 发送过来的信息 "put|filename|filesize|filemd5" :return: """ # 初始化上传文件的基本信息 filename = recv_data.split("|")[1] filesize = int(recv_data.split("|")[2]) filemd5 = recv_data.split("|")[3] # 检查文件是否以前上传过但未传完,用filemd5匹配,返回 check_result = dbapi.check_breakpoint(filemd5, client_user) if check_result[0] == 0: # 不存在断点 break_status = "0" recv_size = 0 # 没有断点的话路径为用户对象当前路径 save_path = os.path.join(client_user.currpath, filename) # 全新的文件的话,更新用户使用空间大小 client_user.update_quota(filesize) else: break_status = "1" recv_size = check_result[0] # 有断点的话,文件路径为上次的文件路径 save_path = check_result[1] # 将状态发送给客户端 ready_status = "{0}|{1}".format(break_status, str(recv_size)) client_socket.send(bytes(ready_status, 'utf8')) try: # 开始接收数据了,每次接收2048 with open(save_path, 'a+b') as fa: fa.seek(recv_size) while filesize - recv_size > 2048: recv_data = client_socket.recv(2048) fa.write(recv_data) recv_size += len(recv_data) # 客户端突然断开了? if recv_data == b'': # 写入文件,为以后断点续传准备 dbapi.write_breakpoint(filemd5, filesize, recv_size, save_path, client_user) break # 剩下的不足2048了 else: # 将剩下的全部收了 recv_data = client_socket.recv(filesize - recv_size) # 客户端突然断开了? if recv_data == b'': # 写入文件,为以后断点续传准备 dbapi.write_breakpoint(filemd5, filesize, recv_size, save_path, client_user) common.writelog("Client upload file connected closed", "error") fa.write(recv_data) # 全部收完了,如果存在断点记录,完成后删除 if break_status == "1": dbapi.del_breakpoint(filemd5, client_user) except Exception as e: if recv_size < filesize: dbapi.write_breakpoint(filemd5, filesize, recv_size, save_path, client_user) common.writelog(str(e), "error")
def handle(self): try: client_socket = self.request client_addr = self.client_address common.writelog("client {0} connected".format(client_addr), "info") # 发送一个成功标识 client_socket.send(bytes("OK", encoding='utf8')) # 定义一个客户端用户对象 client_user = None while True: # 从客户端获取命令信息 recv_client_data = client_socket.recv(100) # 客户端退出了? if recv_client_data == b'': common.writelog("client {0} disconnected".format(client_addr), "info") client_socket.close() break # 取命令(auth,put,get,show,cd) cmd = str(recv_client_data, encoding='utf-8').split("|")[0] common.writelog("client {0} send command {1}".format(client_addr, cmd), "info") # 如果是登录认证 if cmd == "auth": # 获取客户端用户对象 client_user = server.auth(client_socket, str(recv_client_data, 'utf-8')) else: # 如果用户已经登录成功 try: # 通过反射去 module/server 调用命令对应的方法 if hasattr(server, cmd): func = getattr(server, cmd) func(client_socket, client_user, str(recv_client_data, 'utf-8')) else: common.writelog("command {0} function not found".format(cmd), "info") except Exception as e: common.writelog("exec {0} error :{1}".format(cmd, e), "error") client_socket.close() except Exception as e: common.writelog(e, "error")
tmpflag = False while not tmpflag: username = common.input_msg("输入用户名[q返回]: ") if username == "q": tmpflag = True continue # 创建用户对象 new_user = Users(username) if not new_user.exists: userpasswd = common.input_msg("设置初始密码[默认12345]: ", default="12345") totalspace = common.input_msg("设置磁盘配额[默认500M]: ", default=str( settings.HOME_QUOTA)) print("\n 正在初始化用户,请稍等.........\n") new_user.password = common.encry_sha(userpasswd) new_user.islocked = 0 new_user.isdel = 0 new_user.totalspace = int(totalspace) * 1024 * 1024 new_user.usedspace = 0 new_user.create_user() print("初始化成功!") else: print("\033[1;30m用户已经存在!\033[0m;\n") continue except Exception as e: common.writelog("start - main - type2 - {0}".format(e), "error")