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
youku客户端的更多相关文章
- 自学youku_web
仿youku架构 数据库设计 管理员 注册 登录 上传视频 删除视频 发布公告 普通用户 注册 登录 充会员 查看视频 下载免费视频 下载收费视频 查看观影记录 查看公告 思路 class Field ...
- 优酷kux转mp4
利用YouKu客户端下载的视频格式为kux,只能通过YouKu客户端播放,很不方便.在网上看到有人通过ffmpeg解码器进行转换,写成了批处理,如下: @echo off setlocal enabl ...
- [视频]youku与56客户端DLL却持
[视频]youku与56客户端DLL却持 https://pan.baidu.com/s/1c1ArnOk
- HTML5的五种客户端离线存储方案
最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...
- HTML5五种客户端离线存储方案
最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...
- HTML5 Web 客户端五种离线存储方式汇总
最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...
- [开源,学习,分享]UWP第三方简书客户端分享
简介 Windows10正式版发布到现在,我利用零零碎碎的一些时间对UWP进行一些学习,也基于这门技术开发了一个第三方的简书App. 基本界面 优酷视频: http://v.youku.com/v_s ...
- C#应用视频教程1.3 Socket通信客户端完善
我们先把前面的代码封装成一个完整的类,因为跟网络相关的方法并不一定是建立socket的服务器和客户端,所以还是应该把两个分开,比如获取本机IP,修改本机IP,PING远程主机这些事情应该放在一个单独的 ...
- C#应用视频教程1.2 Socket通信客户端实现
接下来我们尝试实现最简单的Socket客户端,为了确保只可能你的代码有问题,服务器要先用别人成熟的代码测试(这也是编程的一个技巧,先不要用自己写的客户端测试自己写的服务器,这样出了问题你也不知道谁有问 ...
随机推荐
- linux父子进程问题
今天遇到一个linux进程启动时指定Max open files不对的问题,导致程序建立socket异常,进而导致fullgc问题,影响正常服务.所以顺带又温习了下linux下的父子进程的特性. 孤儿 ...
- 向Github提交更改的代码
更改了本地的某一文件的代码,那么如何覆盖Github上的同一文件代码呢?请看以下步骤: 1.先用 git status 看你更改了哪些文件: 2.然后 git add 你想要提交的更改的文件 或者 g ...
- dialog problem overview
一 Datasets 1. ubuntu chat corpus: http://daviduthus.org/UCC/2. dialogue datasets (circa 2005) 二 Tas ...
- SQLServer 2008 新增T-SQL 简写语法
1.定义变量时可以直接赋值 DECLARE @Id int = 5 2.Insert 语句可以一次插入多行数据 INSERT INTO StateList VALUES(@Id, 'WA'), (@I ...
- SQL Server ->> T-SQL查询面试题之实例版
1 - 3 题: 数据表结构: OrderID ProductID OrderDate SaleAmount 1 1 2015-01-01 100 2 6 2015-02-01 900 3 1 ...
- SQL Server ->> 与SQL Server服务配置相关的DMV
1) sys.dm_server_services这个DMV可以告诉我们与当前版本的SQL Server相关的服务的启动状态和最后一次启动的时间,诸如这样的信息. SELECT * FROM sys. ...
- java、C语言实现数组模拟栈
java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayS ...
- Thread.Join()的详解
什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序中的一个执行流,每个线程都有自己的专有寄 ...
- [Errno 14] problem making ssl connection Trying other mirror.
使用yum安装程序,报错 解决方法: 我的是升级了下curl就可以了 yum update curl
- Struts2学习-Ioc学习-spring
1.面向对象写法(带着面向过程的思维)电脑 computer = new 电脑(); [电脑代码中 new 打印机()]computer.打印文本("hello 140"); 电脑 ...