def dispatch(massage_json, conn): if massage_json['type'] in func_dic: print('发送来的type', massage_json['type']) func_dic[massage_json['type']](massage_json, conn) else: back_dic = {'flag': False, 'msg': '非合法请求!!'} common.send_back(back_dic, conn)
def login(user_dic, conn): ''' 登录功能,登录成功,将用户信息以{"addr":[session,user_id]}的形式,放到内存中, 多线程操作,必须加锁,锁需要在主线程中生成 :param user_dic: :param conn: :return: ''' user = models.User.select_one(name=user_dic['name']) if user: # 用户存在 if user.user_type == user_dic['user_type']: if user.password == user_dic['password']: session = common.get_uuid(user_dic['name']) da.mutex.acquire() if user_dic['addr'] in da.alive_user: # 如果当前的客户端已经登录,再次登录的时候,把原来的用户踢出,再重新加入进去 da.alive_user.pop(user_dic['addr']) da.alive_user[user_dic['addr']] = [session, user.id] da.mutex.release() back_dic = { 'flag': True, 'session': session, 'is_vip': user.is_vip, 'msg': 'login success' } if user_dic['user_type'] == 'user': last_notice = user_interface.check_notice_by_count(1) back_dic['last_notice'] = last_notice else: back_dic = {'flag': False, 'msg': 'password error'} else: back_dic = {'flag': False, 'msg': '登录类型不匹配'} else: back_dic = {'flag': False, 'msg': 'user do not exisit'} common.send_back(back_dic, conn)
def delete_movie(client): send_dic = { 'type': 'get_movie_list', 'session': admin_data['session'], 'movie_type': 'all' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: while True: movie_list = back_dic['movie_list'] for i, j in enumerate(movie_list): print('%s:%s--%s' % (i, j[0], j[1])) choice = input('please choice movie number to delete>>:').strip() if choice.isdigit(): choice = int(choice) if choice not in range(len(movie_list)): print('not in range') continue send_dic = { 'type': 'delete_movie', 'session': admin_data['session'], 'movie_id': back_dic['movie_list'][choice][2] } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print('must be a number') else: print(back_dic['msg'])
def delete_movie(client): while True: # 先获取服务端没有被删除的电影列表 send_dic = { 'type': 'get_movie_list', 'session': user_dic['cookie'], 'movie_type': 'all' } back_dic = common.send_back(send_dic, client) if back_dic.get('flag'): movie_list = back_dic.get('movie_list') for i, m in enumerate(movie_list): print("%s>>>>>>%s" % (i, m[0])) choice = input("请选择要删除的电影:") if not choice.isdigit(): print("请输入数字!") continue choice = int(choice) if not (choice >= 0 and choice <= len(movie_list)): print("请输入合法编号!") continue send_dic = { 'type': 'delete_movie', 'session': user_dic['cookie'], 'movie_id': movie_list[choice][2] } back_dic = common.send_back(send_dic, client) if back_dic.get('flag'): print(back_dic.get('msg')) else: print(back_dic.get('msg')) else: print(back_dic.get('msg')) break
def check_notice(user_dic, conn): notice_list = check_notice_by_count() if notice_list: back_dic = {'flag': True, 'notice_list': notice_list} else: back_dic = {'flag': False, 'msg': '暂无公告'} common.send_back(back_dic, conn)
def release_notice(user_dic, conn): notice = modles.Notice(name=user_dic['notice_name'], content=user_dic['content'], user_id=user_dic['user_id'], create_time=common.get_nowtime()) back_dic = {'flag': True, 'msg': '发布工作成功!'} common.send_back(back_dic, conn)
def check_movie(user_dic, conn): movie = models.Movie.select_one(file_md5=user_dic['file_md5']) if movie: back_dic = {'flag': True, 'msg': '该影片已存在'} else: back_dic = {'flag': False, 'msg': '该影片不存在,可以上传'} common.send_back(back_dic, conn)
def buy_member(user_dic, conn): user = modles.User.get_obj_by_name(user_dic['name']) user.is_vip = 1 user.save() back_dic = {'flag': True, 'msg': '购买会员成功'} common.send_back(back_dic, conn)
def download_move(user_dic, conn): print('download_movie的user_dic', user_dic) movie = modles.Movie.get_obj_by_name(user_dic['movie_name']) user = modles.User.get_obj_by_name(user_dic['user_id']) # if not user_dic['movie_type'] == 'charge_movie': if user.is_vip: wait_time = 0 else: wait_time = 5 # else: # wait_time = 0 #1 电影名 2 电影大小 3 md5值 movie_name = movie.name movie_path = movie.path movie_size = os.path.getsize(movie_path) send_dic = { 'flag': True, 'movie_name': movie_name, 'movie_size': movie_size, 'file_md5': movie.file_md5, 'wait_time': wait_time, } common.send_back(send_dic, conn) print(user_dic['user_id'], user_dic['movie_name']) download_record = modles.Download_record(name=str(uuid.uuid1()), user_name=user_dic['user_id'], movie_name=user_dic['movie_name']) download_record.save() with open(movie_path, 'rb') as f: for line in f: conn.send(line)
def check_movie_list(massage_json, conn): print('this is check movie') movie_list = get_all_movie() if movie_list: back_movie = [] for movie in movie_list: if not movie.is_delete: if movie.is_free: type = 'Free' else: type = 'Not Free' if massage_json['movie_type'] == 'all': back_movie.append([movie.name, type]) elif massage_json['movie_type'] == 'free': if movie.is_free: back_movie.append([movie.name, type]) else: if not movie.is_free: back_movie.append([movie.name, type]) if back_movie: back_dic = { 'flag': True, 'msg': '查询成功', 'back_movie_list': back_movie } else: back_dic = {'flag': False, 'msg': '暂无电影'} else: back_dic = {'flag': False, 'msg': '暂无电影'} common.send_back(back_dic, conn)
def get_movie_list(user_dic, conn): ''' 获取视频列表:取出全部视频,过滤掉删除的视频,根据前台传来的查询条件,把电影放到列表里 :param user_dic: :param conn: :return: ''' back_dic = {} movie_list = models.Movie.select_all() back_movie_list = [] if movie_list: # 不为空,继续查询,为空直接返回false for movie in movie_list: if not movie.is_delete: # 拼成一个列表['电影名字','收费/免费','电影id'] if user_dic['movie_type'] == 'all': # 全部 back_movie_list.append([ movie.name, '免费' if movie.is_free else '收费', movie.id ]) elif user_dic['movie_type'] == 'free': # 免费电影 if movie.is_free: # 免费的才往列表里放 back_movie_list.append([movie.name, '免费', movie.id]) else: # 收费电影 if not movie.is_free: # 收费的才往列表里放 back_movie_list.append([movie.name, '收费', movie.id]) if back_movie_list: back_dic = {'flag': True, 'movie_list': back_movie_list} else: back_dic = {'flag': False, 'msg': '暂无可查看影片'} else: back_dic = {'flag': False, 'msg': '暂无影片'} common.send_back(back_dic, conn)
def register(user_dic,conn): user=models.User.select_one(name=user_dic['name']) if user: back_dic={'flag':False,'msg':'username has existed'} else: user=models.User(name=user_dic['name'],password=user_dic['password'],user_type=user_dic['user_type']) user.save() back_dic={'flag':True,'msg':'user:%s register successfully'%user_dic['name']} common.send_back(back_dic,conn)
def delete_movie(user_dic, conn): movie = models.Movie.select_one(id=user_dic['movie_id']) movie.is_delete = 1 movie.update() back_dic = { 'flag': True, 'msg': 'movie:%s delete successfully' % movie.name } common.send_back(back_dic, conn)
def release_notice(user_dic, conn): notice = models.Notice(name=user_dic['name'], content=user_dic['content'], user_id=user_dic['user_id']) notice.save() back_dic = { 'flag': True, 'msg': 'notice:%s release successfully' % user_dic['name'] } common.send_back(back_dic, conn)
def upload_movie(client): # 获取本地电影的列 while True: movie_list = common.get_movie_list() if not movie_list: print('暂无本地电影') break for index, movies in enumerate(movie_list, 1): print(index, movies) choice = input('输入电影的编号:').strip() if not choice.isdigit(): continue choice = int(choice) - 1 if choice not in range(len(movie_list)): continue movie_name = movie_list[choice] file_path = os.path.join(settings.LOCAL_MOVIE_PATH, movie_name) # 文件路径 file_md5 = common.get_md5(file_path) send_dic = { 'type': 'check_movie', 'session': user_dic['cookie'], 'file_md5': file_md5 } file_size = os.path.getsize(file_path) back_dic = common.send_back(send_dic, client) if back_dic['flag']: print('选择上传的电影is_free') is_free = input('输入是否是免费<y/>n:').strip() # if is_free == 'y': # is_free =1 # else: # is_free = 0 is_free = 1 if is_free == 'y' else 0 send_dic = { 'type': 'upload_movie', 'session': user_dic['cookie'], 'file_md5': file_md5, 'file_size': file_size, 'movie_name': movie_name, 'is_free': is_free } back_dic = common.send_back(send_dic, client, file_path) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) break else: print(back_dic['msg']) break
def download_charge_movie(client): # 下载收费电影 # 先获取所有电影 while True: send_dic = { 'type': 'get_movie_list', 'session': user_dic['cookie'], 'movie_type': 'charge' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: movie_list = back_dic['movie_list'] # print(movie_list,'999') # [['163cebcf86db5f949ec4ce1ea458b33f668573_150157400119_2.jpg', '收费', 4] for index, movie in enumerate(movie_list, 1): # print(index, movie) print(index, movie) choice = input('输入电影的编号:').strip() if not choice.isdigit(): continue choice = int(choice) - 1 if choice not in range(len(movie_list)): continue movie_name = movie_list[choice][0] # print(movie_name,'777') movie_id = movie_list[choice][2] send_dic = { 'type': 'download_movie', 'session': user_dic['cookie'], 'movie_id': movie_id, 'movie_name': movie_name, 'movie_type': 'charge' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: # 获取文件的大小 文件名 with 上下文管理 file_size = back_dic['file_size'] wait_time = back_dic['wait_time'] # movie_name = back_dic['movie_name'] time.sleep(wait_time) # print(movie_name,'===') file_path = os.path.join(settings.DOWNLOAD_MOVIE, movie_name) # print('开始下载') recv_size = 0 with open(file_path, 'wb') as f: while recv_size < file_size: data = client.recv(1024) f.write(data) recv_size += len(data) print('下载完成') return
def check_download_record(user_dic, conn): #record=user_id,movie_id record_list = models.DownloadRecord.select_all(user_id=user_dic['user_id']) back_movie_list = [] if record_list: for r in record_list: movie = models.Movie.select_one(id=r.movie_id) back_movie_list.append(movie.name) back_dic = {'flag': True, 'download_list': back_movie_list} else: back_dic = {'flag': False, 'msg': '暂无观影记录'} common.send_back(back_dic, conn)
def buy_member(user_dic, conn): ''' 购买会员功能,直接将is_vip字段设为1 :param user_dic: :param conn: :return: ''' user = models.User.select_one(id=user_dic['user_id']) user.is_vip = 1 user.update() back_dic = {'flag': True, 'msg': 'buy success'} common.send_back(back_dic, conn)
def release_notice(user_dic,conn): ''' 发布公告功能,取出字典中的公告名字,公告内容,用户id,存入数据库 :param user_dic: :param conn: :return: ''' notice = models.Notice(name=user_dic['notice_name'], content=user_dic['notice_content'], user_id=user_dic['user_id']) notice.save() back_dic = {'flag': True, 'msg': '公告发布成功'} common.send_back(back_dic, conn)
def check_download_record(user_dic, conn): print(user_dic) all_record_list = get_all_download_record() #获取所有下载记录 record_list = [] #获取该用户的记录对象 for obj in all_record_list: if obj.user_name == user_dic['user_id']: record_list.append(obj) movie_list = [] #获取对象的电影名称 for record in record_list: movie_list.append(record.movie_name) back_dic = {'flag': True, 'msg': '查看成功', 'movie_list': movie_list} common.send_back(back_dic, conn)
def delete_movie(user_dic,conn): ''' 删除视频,不是真正的删除,在视频表中的is_delete字段设为1 :param user_dic: :param conn: :return: ''' movie = models.Movie.select_one(id=user_dic['movie_id']) movie.is_delete = 1 movie.update() back_dic = {'flag': True, 'msg': '电影删除成功'} common.send_back(back_dic, conn)
def down_charge_movie(client): while True: send_dic = { 'type': 'get_movie_list', 'session': user_data['session'], 'movie_type': 'charge' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: for i, j in enumerate(back_dic['movie_list']): print('%s:%s--%s' % (i, j[0], j[1])) choice = input('please choose movie number to download>>;').strip() if choice == 'q': return if choice.isdigit(): choice = int(choice) if choice in range(len(back_dic['movie_list'])): if user_data['is_vip']: buy = input( 'please pay 5 to upload this movie>>:').strip() else: buy = input( 'please pay 10 to buy this movie>>:').strip() if not buy == 'y': return send_dic = { 'type': 'download_movie', 'session': user_data['session'], 'movie_type': 'charge', 'movie_id': back_dic[choice][2] } back_dic = common.send_back(send_dic, client) if back_dic['flag']: recv_size = 0 path = os.path.join(settings.BASE_MOVIE_DOWN, back_dic['file_name']) with open(path, 'wb') as f: while recv_size < back_dic['file_size']: data = client.recv(1024) recv_size += len(data) f.write(data) percent = recv_size / back_dic['file_size'] common.progress(percent) print() else: print(back_dic['msg']) else: print('not in range') else: print('choice must bea number') else: print(back_dic['msg']) break
def check_movie(user_dic,conn): ''' 通过md5校验数据中是否该电影已经存在了 :param user_dic: :param conn: :return: ''' movie = models.Movie.select_one(file_md5=user_dic['file_md5']) if movie: back_dic = {'flag': False, 'msg': '该电影已经存在'} else: back_dic = {'flag': True} common.send_back(back_dic,conn)
def upload_movie(client): up_list = common.get_all_file_by_path(settings.BASE_MOVIE_UP) if up_list: while True: for i, j in enumerate(up_list): print('%s:%s' % (i, j)) choice = input('please choose movie number to upload>>:').strip() if choice == 'q': return if choice.isdigit(): choice = int(choice) if choice not in range(len(up_list)): print('not in range') continue file_name = up_list[choice] file_path = os.path.join(settings.BASE_MOVIE_UP, up_list[choice]) file_size = os.path.getsize(file_path) file_md5 = common.get_big_file_md5(file_path) send_dic = { 'type': 'check_movie', 'session': admin_data['session'], 'file_md5': file_md5 } back_dic = common.send_back(send_dic, client) if not back_dic['flag']: is_free = input('please set movie free or not >>:').strip() if is_free: is_free = 1 else: is_free = 0 send_dic = { 'type': 'upload_movie', 'session': admin_data['session'], 'file_name': file_name, 'file_md5': file_md5, 'file_size': file_size, 'is_free': is_free } back_dic = common.send_back(send_dic, client, file_path) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print(back_dic['msg']) else: print('choose must be a number') else: print('暂无可上传的影片')
def login(massage_json, conn): user = modles.User.get_obj_by_name(name=massage_json['name']) if user: print('这是登陆的user_type', massage_json['user_type']) print('这是这个用户的user_type', user.user_type) if massage_json['user_type'] == user.user_type: if user.password == massage_json['password']: back_dic = {'flag': True, 'msg': '登陆成功'} session = common.get_uuid(massage_json['name']) back_dic['session'] = session #live_user = {addr:[session,user_id],addr:[session,user_id]} user_data.mutex.acquire() user_data.live_user[massage_json['addr']] = [ session, user.name ] user_data.mutex.release() if user.user_type == 'user': back_dic['is_vip'] = user.is_vip back_dic[ 'last_notice'] = user_interface.check_notice_by_count( 1) common.send_back(back_dic, conn) else: back_dic = {'flag': False, 'msg': '密码错误'} common.send_back(back_dic, conn) else: back_dic = {'flag': False, 'msg': '登录类型错误'} common.send_back(back_dic, conn) else: back_dic = {'flag': False, 'msg': '用户不存在'} common.send_back(back_dic, conn)
def check_notice(user_dic, conn): ''' 查看公告功能 :param user_dic: :param conn: :return: ''' # 直接调用通过条数查询的接口,传入None表示全查 notice_list = check_notice_by_count(count=None) if notice_list: back_dic = {'flag': True, 'notice_list': notice_list} else: back_dic = {'flag': False, 'msg': '暂无公告'} common.send_back(back_dic, conn)
def login(client): while True: name = input('输入用户名:').strip() password = input("输入用户密码:").strip() if name and password: send_dic = { 'type': 'login', 'name': name, 'password': common.get_pwd(password), 'user_type': 'user' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) user_dic['cookie'] = back_dic['session'] user_dic['is_vip'] = back_dic['is_vip'] # print(user_dic) # print(back_dic) break else: print(back_dic['msg']) else: print('用户名或密码不正确') break
def check_notice(client): send_dic = {'type': 'check_notice', 'session': user_dic['cookie']} back_dic = common.send_back(send_dic, client) if back_dic.get('flag'): print(back_dic.get('back_list')) else: print(back_dic.get('msg'))
def login(client): if user_dic.get('cookie'): print("您已登录,无需重复登录!") return while True: name = input("请输入用户名:") password = input("请输入密码:") send_dic = { 'type': 'login', 'name': name, 'password': common.get_md5(password), 'user_type': 'user' } back_dic = common.send_back(send_dic, client) if back_dic.get('flag'): for n in back_dic.get('notice'): print(n['title']) print(n['content']) time.sleep(3) print(back_dic.get('msg')) user_dic['cookie'] = back_dic.get('session') user_dic['is_vip'] = back_dic.get('is_vip') break else: print(back_dic.get('msg'))
def register(massage_json, conn): name = massage_json['name'] user = modles.User.get_obj_by_name(name=name) if user: back_dic = {'flag': False, 'msg': '用户存在'} common.send_back(back_dic, conn) else: password = massage_json['password'] user_type = massage_json['user_type'] user = modles.User(name=name, password=password, user_type=user_type, is_vip=0, locked=0) back_dic = {'flag': True, 'msg': '注册成功'} common.send_back(back_dic, conn)