文件結構 config import os IP_PORT = ('127.0.0.1',8080) BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_UPLOAD_MOVIE = os.path.join(BASE_DIR,'up ...
文件結構
config
import os IP_PORT = ('127.0.0.1',8080) BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_UPLOAD_MOVIE = os.path.join(BASE_DIR,'upload_movie') BASE_DOWNLOAD_MOVIE = os.path.join(BASE_DIR,'download_movie')setting
core
import os from tcp_client import tcpclient from lib import common from conf import setting user_state = { 'cookie':None } def admin_register(client): if user_state['cookie']: print('管理員已登錄') return print('\033[32m 註冊功能 \033[0m'.center(10,'-')) while True: name = input('註冊名(q退出)>>>:').strip() if name == 'q':break password = input('註冊密碼>>>:').strip() confirm_password = input('確認註冊密碼>>>:').strip() if password == confirm_password: user_dic = { 'func_type':'register', 'name':name, 'password':common.get_password_md5(password), 'user_type':'admin' } back_dic = common.send_back(user_dic,client) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) continue else: print('兩次密碼不匹配') continue def admin_login(client): if user_state['cookie']: print('管理員已登錄') return print('\033[32m 登錄功能 \033[0m'.center(10, '-')) while True: name = input('登錄名(q退出)>>>:').strip() if name == 'q': break password = input('登錄密碼>>>:').strip() user_dic = { 'func_type': 'login', 'name': name, 'password': common.get_password_md5(password), 'user_type': 'admin' } back_dic = common.send_back(user_dic, client) if back_dic['flag']: user_state['cookie'] = back_dic['session'] print(back_dic['msg']) break else: print(back_dic['msg']) continue @common.login_auth('admin') def upload_movie(client): while True: upload_movie_list = os.listdir(setting.BASE_UPLOAD_MOVIE) if upload_movie_list: for k,movie in enumerate(upload_movie_list): print('%s : %s' % (k,movie)) choice = input('請輸入上傳電影編號(q退出)>>>:').strip() if choice == 'q':break if not choice.isdigit():continue choice = int(choice) if choice >= len(upload_movie_list):continue upload_movie_name = upload_movie_list[choice] upload_movie_path = os.path.join(setting.BASE_UPLOAD_MOVIE, upload_movie_name) upload_movie_size = os.path.getsize(upload_movie_path) upload_movie_md5 = common.get_file_md5(upload_movie_path) send_dic = { 'func_type':'check_upload_movie_exist', 'session':user_state['cookie'], 'file_md5':upload_movie_md5 } back_dic = common.send_back(send_dic,client) if not back_dic['flag']: print(back_dic['msg']) break is_free = input('是否免費(y/n)>>>:').strip() if is_free == 'y': is_free = 1 else: is_free = 0 send_dic = { 'func_type':'upload_movie', 'session':user_state['cookie'], 'file_name':upload_movie_name, 'file_path':upload_movie_path, 'file_size':upload_movie_size, 'is_free':is_free, 'file_md5':upload_movie_md5 } back_dic = common.send_back(send_dic,client,upload_movie_name) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) break else: print('暫無可上傳電影') break @common.login_auth('admin') def delete_movie(client): while True: send_dic = { 'func_type':'check_moive_list', 'session':user_state['cookie'], 'movie_type':'all' } back_dic = common.send_back(send_dic,client) if back_dic['flag']: back_movie_list = back_dic['back_movie_list'] for k,movie in enumerate(back_movie_list): print('%s,影片信息:%s' % (k,movie)) choice = input('選擇刪除的視頻>>>:').strip() if choice == 'q':break if not choice.isdigit():continue choice = int(choice) if choice >= len(back_movie_list):continue delete_movie_item = back_movie_list[choice] del_movie_id = delete_movie_item[0] send_dic = { 'func_type':'delete_movie', 'session':user_state['cookie'], 'del_movie_id':del_movie_id } back_dic = common.send_back(send_dic,client) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print(back_dic['msg']) break @common.login_auth('admin') def release_notice(client): while True: name = input('公告標題(q退出)>>>:').strip() if name == 'q':break content = input('公告內容:>>>:').strip() send_dic = { 'func_type':'release_notice', 'session':user_state['cookie'], 'name':name, 'content':content } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) break func_dic = { '1':admin_register, '2':admin_login, '3':upload_movie, '4':delete_movie, '5':release_notice } def admin_view(): client = tcpclient.get_client() while True: print(''' 1 管理員註冊 2 管理員登錄 3 上傳視頻 4 刪除視頻 5 發佈公告 ''') choice = input('\033[32m 請選擇功能 \033[0m>>>:').strip() if choice == 'q': user_state['cookie'] = None break if not choice.isdigit(): continue if choice not in func_dic: continue func_dic[choice](client)admin
from core import admin,user func_dic = { '1':admin.admin_view, '2':user.user_view } def run(): while True: print(''' 1 管理員視圖 2 用戶視圖 ''') choice = input('\033[32m 請選擇功能 \033[0m>>>:').strip() if choice == 'q':break if not choice.isdigit():continue if choice not in func_dic:continue func_dic[choice]()src
import time import os from tcp_client import tcpclient from lib import common from conf import setting user_state = { 'cookie':None, 'is_vip':None, } def user_register(client): if user_state['cookie']: print('用戶已登錄') return print('\033[32m 註冊功能 \033[0m'.center(10,'-')) while True: name = input('註冊名(q退出)>>>:').strip() if name == 'q':break password = input('註冊密碼>>>:').strip() confirm_password = input('確認註冊密碼>>>:').strip() if password == confirm_password: user_dic = { 'func_type':'register', 'name':name, 'password':common.get_password_md5(password), 'user_type':'user' } back_dic = common.send_back(user_dic,client) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) continue else: print('兩次密碼不匹配') continue def user_login(client): if user_state['cookie']: print('用戶已登錄') return print('\033[32m 登錄功能 \033[0m'.center(10, '-')) while True: name = input('登錄名(q退出)>>>:').strip() if name == 'q': break password = input('登錄密碼>>>:').strip() user_dic = { 'func_type': 'login', 'name': name, 'password': common.get_password_md5(password), 'user_type': 'user' } back_dic = common.send_back(user_dic, client) if back_dic['flag']: user_state['cookie'] = back_dic['session'] user_state['is_vip'] = back_dic['is_vip'] print(back_dic['msg']) print('最新公告:%s' % back_dic['back_notice']) break else: print(back_dic['msg']) continue @common.login_auth('user') def charge_vip(client): while True: if user_state['is_vip']: print('已經是會員') break choice = input('消費10元來充值會員嗎?(y/n)>>>:').strip() if choice == 'y': send_dic = { 'func_type':'charge_vip', 'session':user_state['cookie'], } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) continue else: print('您未充值會員') break @common.login_auth('user') def check_movie(client): if user_state['is_vip']: movie_type = 'all' else: movie_type = 'free' send_dic = { 'func_type':'check_moive_list', 'session':user_state['cookie'], 'movie_type':movie_type } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) print(back_dic['back_movie_list']) return else: print(back_dic['msg']) return @common.login_auth('user') def download_free_movie(client): while True: send_dic = { 'func_type': 'check_moive_list', 'session': user_state['cookie'], 'movie_type': 'free' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) movie_list = back_dic['back_movie_list'] for k,movie in enumerate(movie_list): print('%s : 影片信息:%s' % (k,movie)) choice = input('請選擇下載的電影(q退出)>>>:').strip() if choice == 'q':break if not choice.isdigit():continue choice = int(choice) if choice >= len(movie_list):continue movie_id = movie_list[choice][0] movie_name = movie_list[choice][1] send_dic = { 'func_type':'download_free_movie', 'session':user_state['cookie'], 'movie_id':movie_id } back_dic = common.send_back(send_dic,client) if back_dic['flag']: if back_dic['wait_time']: print('非會員用戶請等待%s後開始下載' % back_dic['wait_time']) time.sleep(back_dic['wait_time']) print('會員用戶您好,已努力載入源數據開始下載:') recv_size = 0 file_size = back_dic['file_size'] file_path = os.path.join(setting.BASE_DOWNLOAD_MOVIE,movie_name) with open(file_path,'wb') as f: while recv_size < file_size: recv_data = client.recv(1024) f.write(recv_data) recv_size += len(recv_data) percent = recv_size / file_size common.print_progress(percent) print('下載成功') break else: print(back_dic['msg']) break break else: print(back_dic['msg']) break @common.login_auth('user') def download_charge_movie(client): while True: send_dic = { 'func_type': 'check_moive_list', 'session': user_state['cookie'], 'movie_type': 'charge' } back_dic = common.send_back(send_dic, client) if back_dic['flag']: print(back_dic['msg']) movie_list = back_dic['back_movie_list'] for k,movie in enumerate(movie_list): print('%s : 影片信息:%s' % (k,movie)) choice = input('請選擇下載的電影(q退出)>>>:').strip() if choice == 'q':break if not choice.isdigit():continue choice = int(choice) if choice >= len(movie_list):continue movie_id = movie_list[choice][0] movie_name = movie_list[choice][1] send_dic = { 'func_type':'download_charge_movie', 'session':user_state['cookie'], 'movie_id':movie_id } back_dic = common.send_back(send_dic,client) if back_dic['flag']: cost_money = back_dic['cost_money'] confirm_charge = input('是否確認消費下載此電影(y/n)>>>:').strip() if confirm_charge == 'n': print('您未下載收費視頻') break print('您已消費金額:%s,電影努力載入中' % cost_money) recv_size = 0 file_size = back_dic['file_size'] file_path = os.path.join(setting.BASE_DOWNLOAD_MOVIE,movie_name) with open(file_path,'wb') as f: while recv_size < file_size: recv_data = client.recv(1024) f.write(recv_data) recv_size += len(recv_data)