优酷客户端
项目目录
conf
import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_MOVIE_PATH = os.path.join(BASE_DIR,'upload_movie') BASE_DOWNLOAD_PATH = os.path.join(BASE_DIR,'download_movie')
core
from TcpClient import tcpclient from lib import common import os from conf import settings user_dic = { 'session':None } def register(client): while True: name = input('please input your username>>>:').strip() password = input('please input your password>>>:').strip() confirm_password = input('please confirm your password>>>:').strip() if password == confirm_password: send_dic = {'type':'register','name':name,'password':common.get_md5(password),'user_type':'admin'} back_dic = common.send_back(client,send_dic) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print('两次密码不一致!') def login(client): while True: name = input('please input your username>>>:').strip() password = input('please input your password>>>:').strip() send_dic = {'type':'login','name':name,'password':common.get_md5(password),'user_type':'admin'} back_dic = common.send_back(client,send_dic) if back_dic['flag']: user_dic['session'] = back_dic['session'] print(back_dic['msg']) break else: print(back_dic['msg']) def upload_movie(client): while True: movie_list = common.get_upload_movie_list() if not movie_list: print('暂无可上传影片') for i,m in enumerate(movie_list): print("%s:%s"%(i+1,m)) choice = input('please choice movie num to upload>>>:').strip() if choice == 'q':break if choice.isdigit(): choice = int(choice) if choice in range(1,len(movie_list)+1): # 获取文件路径 movie_path = os.path.join(settings.BASE_MOVIE_PATH,movie_list[choice-1]) # 获取文件md5值 movie_md5 = common.get_file_md5(movie_path) # 这里拿着md5先去服务端校验是否已有相应视频存在 send_dic = {'type':'check_movie','session':user_dic['session'],'file_md5':movie_md5} back_dic = common.send_back(client,send_dic) if back_dic['flag']: # 电影是否免费 is_free = input('please choice free or not free(y/n)>>:').strip() is_free = 1 if is_free=='y' else 0 # 三元表达式 # 文件大小 file_size = os.path.getsize(movie_path) send_dic = {'type':'upload_movie','session':user_dic['session'],'file_name':movie_list[choice-1], 'file_size':file_size,'file_md5':movie_md5,'is_free':is_free } back_dic = common.send_back(client,send_dic,movie_path) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print(back_dic['msg']) else: print('choice not in range') else: print('please input number') def delete_movie(client): while True: # 先查询出所有没有被删除的电影列表 send_dic = {"type":'get_movie_list','session':user_dic['session'],'movie_type':'all'} # 返回的电影列表可以全部是收费,全部是免费,收费免费都有 back_dic = common.send_back(client,send_dic) if back_dic['flag']: movie_list = back_dic.get('movie_list') for i,m in enumerate(movie_list): print("%s:%s-%s"%(i+1,m[0],m[1])) choice = input('please choice movie number to delete>>>:').strip() if choice == 'q':break if choice.isdigit(): choice = int(choice) if choice in range(1,len(movie_list)+1): send_dic = {"type":"delete_movie",'session':user_dic['session'],'delete_movie_id':movie_list[choice-1][2]} back_dic = common.send_back(client,send_dic) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print('choice not in range') else: print('choice must be a number') else: print(back_dic['msg']) def release_notice(client): while True: title = input('please input notice title>>>:').strip() content = input('please input notice content>>>:').strip() send_dic = {'type':"release_notice",'title':title,'content':content,'session':user_dic.get('session')} back_dic = common.send_back(client,send_dic) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) func_dic = { '1': register, '2': 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('please choice>>>:').strip() if choice not in func_dic: continue func_dic.get(choice)(client)
from core import admin,user func_dic = { '1':admin.admin_view, '2':user.user_view } def run(): while True: print(""" 1.管理员视图 2.用户视图 """) choice = input('please choice>>>:').strip() if choice not in func_dic:continue func_dic.get(choice)()
from TcpClient import tcpclient from lib import common import time import os from conf import settings user_dic = { 'session':None, 'is_vip':None } def register(client): while True: name = input('please input your username>>>:').strip() password = input('please input your password>>>:').strip() confirm_password = input('please confirm your password>>>:').strip() if password == confirm_password: send_dic = {'type': 'register', 'name': name, 'password': common.get_md5(password), 'user_type': 'user'} back_dic = common.send_back(client, send_dic) if back_dic['flag']: print(back_dic['msg']) break else: print(back_dic['msg']) else: print('两次密码不一致!') def login(client): while True: name = input('please input your username>>>:').strip() password = input('please input your password>>>:').strip() send_dic = {'type': 'login', 'name': name, 'password': common.get_md5(password), 'user_type': 'user'} back_dic = common.send_back(client, send_dic) if back_dic['flag']: print(back_dic) user_dic['session'] = back_dic['session'] user_dic['is_vip'] = back_dic['is_vip'] print(back_dic['last_notice']) print(back_dic['msg']) break else: print(back_dic['msg']) def buy_vip(client): if user_dic['is_vip']: print('您已经是会员了,无需再充值') return while True: buy = input('please choice buy or not(y/n)').strip() if buy == 'q':break if buy == 'y': send_dic = {'type':'buy_member','session':user_dic['session']} back_dic = common.send_back(client,send_dic) if back_dic['flag']: user_dic['is_vip'] = 1 print(back_dic['msg']) break else: print(back_dic['msg']) else: print('欢迎下次购买') def check_movie(client): send_dic = {'type':'get_movie_list','session':user_dic['session'],'movie_type':'all'} back_dic = common.send_back(client,send_dic) if back_dic['flag']: back_movie_list = back_dic['movie_list'] for i,m in enumerate(back_movie_list): print('%s:%s-%s'%(i+1,m[0],m[1])) return else: print(back_dic['msg']) def download_free_movie(client): # 先拿到所有的免费电影 send_dic = {'type':'get_movie_list','session':user_dic['session'],'movie_type':'free'} back_dic = common.send_back(client,send_dic) if back_dic['flag']: movie_list = back_dic['movie_list'] for i,m in enumerate(movie_list): print('%s:%s-%s'%(i+1,m[0],m[1])) choice = input('please choice movie number to download>>>:').strip() if choice.isdigit(): choice = int(choice) if choice in range(1,len(movie_list)+1): send_dic = {'type':'download_movie','session':user_dic['session'],'movie_id':movie_list[choice-1][2],'movie_type':'free'} back_dic = common.send_back(client,send_dic) if back_dic['flag']: print('请等待>>>') time.sleep(back_dic['wait_time']) file_path = os.path.join(settings.BASE_DOWNLOAD_PATH,back_dic['file_name']) recv_size = 0 with open(file_path,'wb') as f: while recv_size < back_dic['file_size']: recv_data = client.recv(1024) f.write(recv_data) recv_size += len(recv_data) print('下载成功!') else: print(back_dic['msg']) else: print('choice not in range') else: print('choice must be a number') else: print(back_dic['msg']) def download_charge_movie(client): # 针对普通用户和vip用户下载收费视频收费标准不一样 if user_dic['is_vip']: charge = input('please pay 5$ to buy(y/n)>>>:').strip() else: charge = input('please pay 10$ to buy(y/n)>>>:').strip() if not charge == 'y': print('欢迎下次付钱下载') return send_dic = {'type':'get_movie_list','session':user_dic['session'],'movie_type':'charge'} back_dic = common.send_back(client,send_dic) if back_dic['flag']: movie_list = back_dic['movie_list'] for i,m in enumerate(movie_list): print('%s:%s-%s'%(i+1,m[0],m[1])) choice = input('please choice movie number to download>>>:').strip() if choice.isdigit(): choice = int(choice) if choice in range(1,len(movie_list)+1): send_dic = {'type': 'download_movie', 'session': user_dic['session'], 'movie_id': movie_list[choice - 1][2], 'movie_type': 'charge'} back_dic = common.send_back(client, send_dic) if back_dic['flag']: file_path = os.path.join(settings.BASE_DOWNLOAD_PATH, back_dic['file_name']) recv_size = 0 with open(file_path, 'wb') as f: while recv_size < back_dic['file_size']: recv_data = client.recv(1024) f.write(recv_data) recv_size += len(recv_data) print('下载成功!') return else: print(back_dic['msg']) else: print('choice not in range') else: print('choice must be a number') else: print(back_dic['msg']) def check_download_record(client): send_dic = {'type':'check_download_record','session':user_dic['session']} back_dic = common.send_back(client,send_dic) if back_dic['flag']: print(back_dic['record']) return else: print(back_dic['msg']) def check_notice(client): send_dic = {'type':'check_notice','session':user_dic['session']} back_dic = common.send_back(client,send_dic) if back_dic['flag']: print(back_dic['notice_list']) return else: print('暂无公告') func_dic = { '1': register, '2': login, '3': buy_vip, '4': check_movie, '5': download_free_movie, '6': download_charge_movie, '7': check_download_record, '8': check_notice } def user_view(): client = tcpclient.get_client() while True: print(""" 1 注册 2 登录 3 冲会员 4 查看视频 5 下载免费视频 6 下载收费视频 7 查看观影记录 8 查看公告 """) choice = input('please choice>>>:').strip() if choice not in func_dic: continue func_dic.get(choice)(client)
lib
import json import hashlib import struct from conf import settings import os def send_back(client, send_dic, file=None): send_bytes = json.dumps(send_dic).encode('utf-8') client.send(struct.pack('i', len(send_bytes))) client.send(send_bytes) # 如果file参数有值,表示除了传数据之外,还需要传文件 if file: with open(file, 'rb') as f: for line in f: client.send(line) back_header = client.recv(4) back_bytes = client.recv(struct.unpack('i', back_header)[0]) back_dic = json.loads(back_bytes.decode('utf-8')) return back_dic def get_md5(password): md = hashlib.md5() md.update(password.encode('utf-8')) return md.hexdigest() def get_upload_movie_list(): movie_list = os.listdir(settings.BASE_MOVIE_PATH) return movie_list def get_file_md5(file_path): if os.path.exists(file_path): md = hashlib.md5() file_size = os.path.getsize(file_path) # 这里切片获取大文件固定几个位置的数据,节省大文件md5值获取时间 file_list = [0, file_size // 3, (file_size // 3) * 2, file_size - 10] with open(file_path, 'rb') as f: for line in file_list: f.seek(line) md.update(f.read(10)) return md.hexdigest()
TcpClient
import socket def get_client(): client = socket.socket() client.connect(('127.0.0.1',8082)) return client
import os import sys BASE_DIR = os.path.dirname(__file__) sys.path.append(BASE_DIR) from core import src if __name__ == '__main__': src.run()