youku客户端

文件结构

youku客户端

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 = {
'':admin_register,
'':admin_login,
'':upload_movie,
'':delete_movie,
'':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 = {
'':admin.admin_view,
'':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)
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 check_download_record(client):
send_dic = {
'func_type':'check_download_record',
'session':user_state['cookie']
}
back_dic = common.send_back(send_dic, client)
if back_dic['flag']:
print(back_dic['msg'])
print(back_dic['back_download_record_list'])
return
else:
print(back_dic['msg'])
return @common.login_auth('user')
def check_notice(client):
send_dic = {
'func_type': 'check_notice',
'session': user_state['cookie']
}
back_dic = common.send_back(send_dic, client)
if back_dic['flag']:
print(back_dic['msg'])
print(back_dic['notice_list'])
return
else:
print(back_dic['msg'])
return func_dic = {
'':user_register,
'':user_login,
'':charge_vip,
'':check_movie,
'':download_free_movie,
'':download_charge_movie,
'':check_download_record,
'':check_notice
} def user_view():
client = tcpclient.get_client()
while True:
print('''
1 用户注册
2 用户登录
3 充会员
4 查看视频
5 下载免费视频
6 下载收费视频
7 查看下载记录
8 查看公告
''') choice = input('\033[32m 请选择功能 \033[0m>>>:').strip()
if choice == 'q':
user_state['cookie'] = None
user_state['is_vip'] = 0
break
if not choice.isdigit(): continue
if choice not in func_dic: continue
func_dic[choice](client)

user

lib

import hashlib
import json
import struct
import os def login_auth(user_type):
from core import admin,user
def wrapper(func):
def auth(*args,**kwargs):
if user_type == 'admin':
if not admin.user_state['cookie']:
print('请先登录')
admin.admin_login(args[0])
else:
func(*args,**kwargs)
elif user_type == 'user':
if not user.user_state['cookie']:
print('请先登录')
user.user_login(args[0])
else:
func(*args,**kwargs)
else:
return False
return auth
return wrapper def get_password_md5(password):
md = hashlib.md5()
md.update('密码加盐'.encode('utf-8'))
md.update(password.encode('utf-8'))
return md.hexdigest() def send_back(send_dic,client,file=None):
# 发送
send_dic_json = json.dumps(send_dic)
send_dic_bytes = send_dic_json.encode('utf-8')
send_dic_head = struct.pack('i',len(send_dic_bytes))
client.send(send_dic_head)
client.send(send_dic_bytes) # 文件
if file:
send_size = 0
file_size = send_dic['file_size']
with open(send_dic['file_path'],'rb') as f:
while send_size < file_size:
send_data = f.readline()
client.send(send_data)
send_size += len(send_data)
percent = send_size / file_size
print_progress(percent) # 接收
back_dic_head = client.recv(4)
back_dic_len = struct.unpack('i',back_dic_head)[0]
back_dic_bytes = client.recv(back_dic_len)
back_dic_json = back_dic_bytes.decode('utf-8')
back_dic = json.loads(back_dic_json)
return back_dic def print_progress(percent):
width = 70
sign = '■'
if percent > 1:
percent = 1
print_str = '[%%-%ds]' % width % (int(percent * width) * sign)
print('\r%s %d%%' % (print_str,int(percent * 100)),end='') # 大文件校验唯一性的md5值
def get_file_md5(file_path):
md = hashlib.md5()
if os.path.exists(file_path):
file_size = os.path.getsize(file_path)
file_pieces_list = [0, file_size // 3, (file_size // 3) * 2, file_size - 10]
with open(file_path, 'rb') as f:
for file_piece in file_pieces_list:
f.seek(file_piece)
md.update(f.read(10))
return md.hexdigest()
else:
print('文件不存在')
return False if __name__ == '__main__':
print(get_file_md5('/Users/limengjie/Desktop/pyhon/youku_web/youku_client/upload_movie/test.pdf'))
print(get_file_md5('/Users/limengjie/Desktop/pyhon/youku_web/youku_client/upload_movie/test2.pdf')) percent = 0
while percent < 1:
percent += 0.1
print_progress(percent)

common

tcp_client

import socket
from conf import setting def get_client():
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(setting.IP_PORT)
return client

tcpclient

根目录下:

import os
import sys path = os.path.dirname(__file__)
sys.path.append(path) from core import src if __name__ == '__main__':
src.run()

start

上一篇:常用jquery记录


下一篇:An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习