文件结构

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客户端的更多相关文章

  1. 自学youku_web

    仿youku架构 数据库设计 管理员 注册 登录 上传视频 删除视频 发布公告 普通用户 注册 登录 充会员 查看视频 下载免费视频 下载收费视频 查看观影记录 查看公告 思路 class Field ...

  2. 优酷kux转mp4

    利用YouKu客户端下载的视频格式为kux,只能通过YouKu客户端播放,很不方便.在网上看到有人通过ffmpeg解码器进行转换,写成了批处理,如下: @echo off setlocal enabl ...

  3. [视频]youku与56客户端DLL却持

    [视频]youku与56客户端DLL却持 https://pan.baidu.com/s/1c1ArnOk

  4. HTML5的五种客户端离线存储方案

    最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...

  5. HTML5五种客户端离线存储方案

    最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...

  6. HTML5 Web 客户端五种离线存储方式汇总

    最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...

  7. [开源,学习,分享]UWP第三方简书客户端分享

    简介 Windows10正式版发布到现在,我利用零零碎碎的一些时间对UWP进行一些学习,也基于这门技术开发了一个第三方的简书App. 基本界面 优酷视频: http://v.youku.com/v_s ...

  8. C#应用视频教程1.3 Socket通信客户端完善

    我们先把前面的代码封装成一个完整的类,因为跟网络相关的方法并不一定是建立socket的服务器和客户端,所以还是应该把两个分开,比如获取本机IP,修改本机IP,PING远程主机这些事情应该放在一个单独的 ...

  9. C#应用视频教程1.2 Socket通信客户端实现

    接下来我们尝试实现最简单的Socket客户端,为了确保只可能你的代码有问题,服务器要先用别人成熟的代码测试(这也是编程的一个技巧,先不要用自己写的客户端测试自己写的服务器,这样出了问题你也不知道谁有问 ...

随机推荐

  1. 数据结构与算法C语言所有头文件汇总 —— 持续更新

    header.h // 顺序表的结构定义 #define Maxsize 100 //const int Maxsize = 100; // 预先定义一个足够大的常数 typedef struct { ...

  2. MongoDB 创建集合

    createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 createCollection() ...

  3. linux下通过NFS将远程磁盘mount到本地

    最近由于项目原因需要和其他两家公司对接,需要取对方服务器中的图像数据,原本约定是三方都通过http协议来进行通讯,奈何对接方不配合,说文件就在他们服务器放着,怎么取他们不管.所以采取将对方服务器磁盘挂 ...

  4. 实验三:klee的执行重现机制(示例分析)

    结论性内容: (1)如果是在程序中使用klee_make_symbolic,则可以使用下列脚本进行重现. export LD_LIBRARY_PATH=/home/klee/xiaojiework/k ...

  5. Java Collections Framework知识结构目录

    The core collection interfaces are the foundation of the Java Collections Framework. The Java Collec ...

  6. js if语句只写一个参数是什么意思?

    如 var a=0:if(!a){...}; avascript中以下值会被转换为false false undefined null 0 -0 NaN ""

  7. WebRequest的get及post提交

    static string get_html(string url) { var request = WebRequest.Create(url); var response = request.Ge ...

  8. 【Leetcode】【Medium】Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  9. ARC以及MRC中setter方法的差异

    ARC以及MRC中setter方法的差异 有时候,你会需要重写setter或者getter方法,你知道么,ARC与MRC的setter方法是有着差异的呢. 先看下MRC下的setter方法: 在看下A ...

  10. window搭建svn服务器,本地提交至服务器后,直接同步

    找到版本库目录(在安装svnserver时指定的目录),如下图指定了一个版本库的hooks 在其中创建post-commit.bat文件(可先创建post-cmmit.txt再修改后缀名为bat). ...