def interlacing_color(self, custom_list): # 列表隔行换色 diff = 0 # 差值 if len(custom_list) > len(Prompt.colour_dic): # 当菜单列表长度大于颜色列表长度时 diff = len(custom_list) - len(Prompt.colour_list) # 菜单列表长度-颜色列表长度 colour_list = list(Prompt.colour_dic) new_list = colour_list # 新的颜色列表 if diff >= 0: # 当菜单列表长度大于等于颜色列表长度时 for i in range(diff + 1): new_list.append(colour_list[i]) # 添加颜色,使颜色列表等于菜单列表长度 count = -1 # 颜色列表索引值,默认为-1 for key, item in enumerate(custom_list, 1): # 获取每个角色类的operate_lst静态属性,显示的序列号从1开始 count += 1 # 索引加1 if type(item) == str: ret = Prompt.random_color('{}.\t{}'.format(key, item)) # 随机显示颜色 print(ret) else: length = len(item) if length == 2: ret = Prompt.display('{}.\t{}'.format(key, item[0]), new_list[count]) # 按照列表顺序显示颜色 print(ret) elif length == 5: ret = Prompt.display('{}.\t{}'.format( key, item['username']), item['color']) # 按照列表顺序显示颜色 print(ret)
def run_chat(self, friends, friends_color): # 发起聊天 while True: inp = input('请输入发送内容,或者输入b返回: ').strip() if not inp: print(Prompt.display('发送内容不能为空!', 'red')) continue elif inp.upper() == 'B': pro_mes = Prompt.display('是否关闭当前会话 Y/N ? ', 'red') confirm = input(pro_mes).strip() if confirm.upper() == 'Y': self.menu() self.sk.close() break elif confirm.upper() == 'N': continue else: print(Prompt.display('输入内容有误,请输入Y/N', 'red')) continue self.sk.my_send(('{}:{}:{}:{}:{}'.format( self.info['username'], self.info['color'], friends, friends_color, inp)), (self.ip, self.port)) # 发送数据给服务器 print(Prompt.display('正在等待对方回复...', 'green')) msg, addr = self.sk.my_recv(self.rxb) # 接收信息 name, color, mesg = msg.split(':') # 姓名,颜色,消息 print(''.center(36, '=')) print(time.strftime('%Y-%m-%d %H:%M:%S')) print(Prompt.display('❀{} : {}'.format(name, mesg), color)) self.sk.close() # 关闭连接
def show_my_user(self): # 查看我的好友 if self.user_info()['friends'] == []: print(Prompt.display('当前好友暂无!请添加好友', 'red')) else: friends_list = self.user_info()['friends'] print('好友列表如下:'.center(31, '=')) self.interlacing_color(friends_list) print(''.center(36, '=')) while True: pro_mes = Prompt.display('是否发起聊天 Y/N(返回) ? ', 'green') confirm = input(pro_mes).strip() if confirm.upper() == 'Y': self.interlacing_color(friends_list) choice = input('请输入好友编号,或输入b返回: ').strip() if choice.upper() == 'B': # self.menu() break if choice.isdigit(): choice = int(choice) if choice in range(1, len(friends_list) + 1): friends = friends_list[choice - 1] # 好友 color = self.friends_color(friends) # 好友颜色 self.run_chat(friends, color) # 发起会话 else: print(Prompt.display('好友编号不存在,请重新输入!', 'red')) continue else: print(Prompt.display('请输入数字!', 'red')) continue elif confirm.upper() == 'N': break else: print(Prompt.display('输入错误,请重新输入!', 'red')) continue
def delete_my_user(self): # 删除好友 friends_list = self.user_info()['friends'] # 获取当前用户好友列表 print('好友列表如下:'.center(31, '=')) self.interlacing_color(friends_list) # 显示列表 print(''.center(36, '=')) while True: choice = input('请输入好友编号,或输入b返回: ').strip() if choice.upper() == 'B': # self.menu() break if choice.isdigit(): choice = int(choice) if choice in range(1, len(friends_list) + 1): friends = friends_list[choice - 1] # 选择的好友 pro_mes = Prompt.display('您真的要删除吗? Y/N ', 'red') confirm = input(pro_mes).strip() if confirm.upper() == 'Y': ret = self.delete(friends) # 执行删除动作 if ret is False: print(Prompt.display('删除好友失败!', 'red')) else: print(Prompt.display('删除好友成功!', 'green')) break else: print(Prompt.display('请输入数字!', 'red'))
def add_my_user(self): # 添加好友 friends = self.choose_friends() # 选择所有用户 ret = self.add(friends[0]) # 添加指定的用户 if ret is False: print(Prompt.display('添加好友失败!', 'red')) else: print(Prompt.display('添加好友成功!', 'green'))
def login(self): # 判断失败次数是否小于等于最大失败次数 while self.auth_dic['failures'] <= self.auth_dic['maximum']: username = input('请输入登录用户名: ').strip() if not username: print(Prompt.display('用户名不能为空!', 'red')) continue password = input('请输入登录密码: ').strip() if not password: print(Prompt.display('密码不能为空!', 'red')) continue en_pwd = self.get_pwd(username, password) # 获取加密密码 # 判断用户名和密码是否一致 res = self.user_auth(username, en_pwd) if res['msg']: print(Prompt.display('登陆成功!', 'green')) Logger.logger.info('%s 登陆成功' % username) # 修改初始变量 self.auth_dic['username'] = username self.auth_dic['status'] = True self.auth_dic['color'] = res['color'] return {'username': username, 'color': res['color']} # 返回字典给调用者 else: chance = self.auth_dic['maximum'] - self.auth_dic['failures'] # 剩余失败次数 print(Prompt.display('用户或密码错误,请重新输入!您还有%s次机会!'%chance, 'red')) self.auth_dic['failures'] += 1 # 失败次数加1 Logger.logger.info('%s 登陆失败%s次' % (username,self.auth_dic['maximum'] - chance)) # 判断失败次数大于最大次数时,直接退出! if self.auth_dic['failures'] > self.auth_dic['maximum']: self.auth_dic['flag'] = False return False
def main(self): sk = Mysocket() sk.bind((self.ip, self.port)) while True: print(Prompt.display('正在等待对方回复...', 'green')) msg, client_addr = sk.my_recv(self.rxb) # udp协议不用建立链接 name, color, friends, friends_color, mesg = msg.split(':') print(''.center(36, '=')) print(time.strftime('%Y-%m-%d %H:%M:%S')) print(Prompt.display('{} : {}'.format(name, mesg), color.strip())) inp = input('>>>') sk.my_send('{}:{}:{}'.format(friends, friends_color, inp), client_addr) # 发送信息 sk.close()
def user_auth(username, password): ''' #判断用户名和密码是否匹配 :param username: 用户名 :param password: 密码 :return: True 匹配成功 False 匹配失败 ''' if not username or not password: print(Prompt.display('用户名或者密码不能为空!', 'red')) return False
def choose_friends(self): # 选择好友,进行相应的操作 self.show_user() # 显示所有用户 while True: choice = input('请输入用户编号,或输入b返回: ').strip() if choice.upper() == 'B': self.menu() break if choice.isdigit(): choice = int(choice) if choice in range(1, len(self.user_all()) + 1): friends = self.user_all()[choice - 1] return friends['username'], friends['color'] else: print(Prompt.display('请输入数字!', 'red'))
def add(self, friends): # 添加动作 if not friends: return '好友名字不能为空!' if friends in self.user_info()['friends']: print(Prompt.display('好友已添加,请勿重复!', 'red')) return False user_list = [] # 所有用户列表 for i in self.user_files.load(): if i['username'] == self.info['username']: # 如果是当前用户 i['friends'].append(friends) # 追加到好友列表中 user_list.append(i) ret = self.write_file(user_list) # 写入文件 if ret is False: return False else: return True
def user_auth(username, password): ''' #判断用户名和密码是否匹配 :param username: 用户名 :param password: 密码 :return: True 匹配成功 False 匹配失败 ''' if not username or not password: print(Prompt.display('用户名或者密码不能为空!', 'red')) return False user = MyPickle(settings.file_name['user']) read_user = user.load() for i in read_user: if username == i['username'] and password == i['password']: result = {'msg': True, 'color': i['color']} return result else: return {'msg': False, 'color': None}
def delete_files(self): print(Prompt.display('功能未实现,敬请期待!', 'green'))
def download_file(self): print(Prompt.display('功能未实现,敬请期待!', 'green'))
def see_file(self): print(Prompt.display('功能未实现,敬请期待!', 'green'))
def main(self): print( Prompt.display('您好: {} 欢迎使用聊天系统!\n'.format(self.username), 'purple_red')) self.menu()