优酷程序

client:

setting

# 表创建
​```python
create table User(
    u_id int auto_increment primary key,
    username varchar(64),
    password varchar(64),
    is_vip int not null default 0,
    user_type varchar(64),
    register_time varchar(64)
)engine=Innodb charset='utf8';

create table Movie(
    m_id int auto_increment primary key,
    movie_name varchar(64),
    movie_path varchar(255),
    movie_size varchar(64),
    is_free int not null default 1,
    is_delete int not null default 0,
    upload_time varchar(64),
    file_md5 varchar(64),
    u_id int
)engine=Innodb charset='utf8';

create table Notice(
    n_id int auto_increment primary key,
    title varchar(64),
    content varchar(255),
    u_id int,
    create_time varchar(255)
)engine=Innodb charset='utf8';

create table DownloadRecord(
    d_id int auto_increment primary key,
    u_id int,
    m_id int,
    download_time varchar(255)
)engine=Innodb charset='utf8';
create table download_record(
    d_id int auto_increment primary key,
    u_id int,
    m_id int,
    download_time varchar(255)
)engine=Innodb charset='utf8';
​```

#core --> src


from core import admin, user

func_dic = {
    '1': admin.admin_view,
    '2': user.user_view
}

def run():
    print('启动客户端...')
    while True:
        print('''
            优酷系统:
                1.管理员视图
                2.普通用户视图
                q.退出
            ''')

        choice = input('请输入用户类型编号:').strip()

        if choice == 'q':
            break

        if choice not in func_dic:
            continue

        func_dic.get(choice)()

admin

from socket_client import tcp_client
from lib import common
from conf import settings
import os

user_info = {
    'cookies': None
}

def register(client):
    while True:
        username = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()
        re_password = input('请确认密码:').strip()
        if password == re_password:
            send_dic = {
                'type': 'register',
                'username': username,
                'password': password,
                'user_type': 'admin',
            }

            back_dic = common.send_and_back(send_dic, client)

            if back_dic.get('flag'):
                print(back_dic.get('msg'))
                break

            else:
                print(back_dic.get('msg'))
        else:
            print('密码不一致')

def login(client):
    while True:
        username = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()

        send_dic = {
            'type': 'login',
            'username': username,
            'password': password,
            'user_type': 'admin'
        }

        back_dic = common.send_and_back(send_dic, client)

        if back_dic.get('flag'):
            print(back_dic.get('msg'))
            user_info['cookies'] = back_dic.get('session')
            break

        else:
            print(back_dic.get('msg'))

def upload_movie(client):

    while True:
        movie_list = common.get_movie_list()
        if not movie_list:
            print('没有电影可上传...')
            break

        for index, movie_name in enumerate(movie_list):
            print(index, movie_name)

        choice = input('请选择上传的电影编号:').strip()

        if choice == 'q':
            break

        if not choice.isdigit():
            print('请输入数字')
            continue

        choice = int(choice)

        if choice not in range(len(movie_list)):
            print('选择错误,请重新输入..')
            continue

        movie_name = movie_list[choice]

        movie_path = os.path.join(
            settings.UPLOAD_MOVIE_PATH, movie_name
        )

        movie_size = os.path.getsize(movie_path)

        file_md5 = common.get_movie_md5(movie_path)

        send_dic = {
            'type': 'check_movie',
            'cookies': user_info.get('cookies'),
            'file_md5': file_md5
        }

        back_dic = common.send_and_back(send_dic, client)

        if not back_dic.get('flag'):
            print(back_dic.get('msg'))
            continue
        is_free = input('请确认是否免费(y/n)').strip()

        number = 0

        if is_free == 'y':
            number = 1

        send_dic = {
            'type': 'upload_movie',
            'cookies': user_info.get('cookies'),
            'file_md5': file_md5,
            'movie_name': movie_name,
            'movie_size': movie_size,
            'is_free': number
        }

        back_dic = common.send_and_back(
            send_dic, client, file=movie_path)

        if back_dic.get('flag'):
            print(back_dic.get('msg'))
            break

def delete_movie(client):
    flag = True
    while flag:
        send_dic = {
            'type': 'get_movie_list',
            'cookies': user_info.get('cookies'),
            'movie_type': 'all'
        }

        back_dic = common.send_and_back(send_dic, client)

        if not back_dic.get('flag'):
            print(back_dic.get('msg'))
            break

        while flag:
            movie_list = back_dic.get('movie_list')
            for index, movie_s in enumerate(movie_list):
                print(index, movie_s)

            choice = input('请输入删除的电影编号: ').strip()

            if choice == 'q':
                flag = False

            if not choice.isdigit():
                print('请输入数字')
                continue

            choice = int(choice)

            if choice not in range(len(movie_list)):
                print('请输入正确编号')
                continue
            _, movie_id, _ = movie_list[choice]

            send_dic = {
                'type': 'delete_movie',
                'cookies': user_info.get('cookies'),
                'movie_id': movie_id
            }

            back_dic = common.send_and_back(send_dic, client)

            if back_dic.get('flag'):
                print(back_dic.get('msg'))
                flag = False

def send_notice(client):
    title = input('请输入公告标题:').strip()
    content = input('请输入公告内容:').strip()

    send_dic = {
        'type': 'send_notice',
        'cookies': user_info.get('cookies'),
        'title': title,
        'content': content
    }

    back_dic = common.send_and_back(
        send_dic, client)

    print(back_dic.get('msg'))


func_dic = {
    '1': register,
    '2': login,
    '3': upload_movie,
    '4': delete_movie,
    '5': send_notice,
}

def admin_view():
    client = tcp_client.get_client()
    while True:
        print('''
                1.注册
                2.登录
                3.上传视频
                4.删除视频
                5.发布公告
        ''')
        choice = input('请输入功能编号:').strip()

        if choice == 'q':
            break

        if choice not in func_dic:
            continue

        func_dic.get(choice)(client)

user

from socket_client import tcp_client
from lib import common
from conf import settings
import os
import time
user_info = {
    'cookies': None,
    'is_vip': None
}

def register(client):
    while True:
        username = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()
        re_password = input('请确认密码:').strip()
        if password == re_password:
            send_dic = {
                'type': 'register',
                'username': username,
                'password': password,
                'user_type': 'user',
            }

            back_dic = common.send_and_back(send_dic, client)

            if back_dic.get('flag'):
                print(back_dic.get('msg'))
                break

            else:
                print(back_dic.get('msg'))
        else:
            print('密码不一致')

def login(client):
    while True:
        username = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()

        send_dic = {
            'type': 'login',
            'username': username,
            'password': password,
            'user_type': 'user'
        }

        back_dic = common.send_and_back(send_dic, client)

        if back_dic.get('flag'):
            print(back_dic.get('msg'))
            user_info['cookies'] = back_dic.get('session')

            user_info['is_vip'] = back_dic.get('is_vip')
            break

        else:
            print(back_dic.get('msg'))

def check_movies(client):
    send_dic = {
        'type': 'get_movie_list',
        'cookies': user_info.get('cookies'),
        'movie_type': 'all'
    }

    back_dic = common.send_and_back(send_dic, client)

    if back_dic.get('flag'):
        print(back_dic.get('movie_list'))

    else:
        print(back_dic.get('msg'))

def pay_vip(client):
    is_vip = input('输入y确认购买VIP,否则购买失败:').strip()

    if is_vip == 'y':
        send_dic = {
            'type': 'pay_vip',
            'cookies': user_info.get('cookies')
        }

        back_dic = common.send_and_back(send_dic, client)

        print(back_dic.get('msg'))
    else:
        print('欢迎下次购买!!')


def download_free_movie(client):
    flag = True
    while flag:
        send_dic = {
            'type': 'get_movie_list',
            'cookies': user_info.get('cookies'),
            'movie_type': 'free'
        }

        back_dic = common.send_and_back(send_dic, client)

        free_movie_list = back_dic.get('movie_list')
        if free_movie_list:

            for index, movie_list in enumerate(free_movie_list):
                print(index, movie_list)
            choice = input('请选择电影编号:').strip()

            if choice == 'q':
                break

            if not choice.isdigit():
                print('请输入数字')
                continue

            choice = int(choice)

            if choice not in range(len(free_movie_list)):
                print('请输入正确编号')
                continue
            movie_list = free_movie_list[choice]

            send_dic = {
                'type': 'download_movie',
                'cookies': user_info.get('cookies'),
                'movie_id': movie_list[1]
            }

            back_dic = common.send_and_back(send_dic, client)

            if back_dic.get('flag'):
                movie_name = back_dic.get('movie_name')
                movie_size = back_dic.get('movie_size')

                movie_path = os.path.join(
                    settings.DOWNLOAD_MOVIES_PATH,
                    movie_name
                )
                if not user_info.get('is_vip'):
                    print('广告时间.........')
                    time.sleep(6)
                    print('请开始你的观看...')

                recv_data = 0
                with open(movie_path, 'wb') as f:
                    while recv_data < int(movie_size):
                        data = client.recv(1024)
                        f.write(data)
                        recv_data += len(data)

                print(f'电影[{movie_name}]下载成功...')
                break

        else:
            flag = False
            print('没有免费电影...')


def download_pay_movie(client):
    flag = True
    while flag:
        send_dic = {
            'type': 'get_movie_list',
            'cookies': user_info.get('cookies'),
            'movie_type': 'pay'
        }

        back_dic = common.send_and_back(send_dic, client)

        pay_movie_list = back_dic.get('movie_list')
        if pay_movie_list:

            for index, movie_list in enumerate(pay_movie_list):
                print(index, movie_list)
            choice = input('请输入要下载的电影编号:').strip()

            if choice == 'q':
                break

            if not choice.isdigit():
                print('请输入数字')
                continue

            choice = int(choice)

            if choice not in range(len(pay_movie_list)):
                print('请输入正确编号')
                continue
            movie_list = pay_movie_list[choice]

            if user_info.get('is_vip'):
                sure = input('确认购买,会员价打半折!!!: ').strip()
            else:
                sure = input('大众用户一口价8888: ').strip()

            if sure != 'y':
                print('欢迎下次下载,等你哦!!')
                break

            else:
                print('已经在你的空间等待主人你的进入!!!')

            send_dic = {
                'type': 'download_movie',
                'cookies': user_info.get('cookies'),
                'movie_id': movie_list[1]
            }

            back_dic = common.send_and_back(send_dic, client)

            if back_dic.get('flag'):
                movie_name = back_dic.get('movie_name')
                movie_size = back_dic.get('movie_size')
                movie_path = os.path.join(
                    settings.DOWNLOAD_MOVIES_PATH,
                    movie_name
                )

                recv_data = 0
                with open(movie_path, 'wb') as f:
                    while recv_data < int(movie_size):
                        data = client.recv(1024)
                        f.write(data)
                        recv_data += len(data)

                print(f'电影[{movie_name}]下载成功...')
                break

        else:
            flag = False
            print('没有免费电影...')

def check_download_record(client):
    send_dic = {
        'type': 'check_record',
        'cookies': user_info.get('cookies'),
    }

    back_dic = common.send_and_back(send_dic, client)

    if back_dic.get('flag'):
        print(back_dic.get('record_list'))
    else:
        print(back_dic.get('msg'))


def check_notice(client):

    send_dic = {
        'type': 'check_notice',
        'cookies': user_info.get('cookies')
    }

    back_dic = common.send_and_back(send_dic, client)

    if back_dic.get('flag'):
        print(back_dic.get('notice_list'))

    else:
        print(back_dic.get('msg'))


func_dic = {
    '1': register,
    '2': login,
    '3': check_movies,
    '4': pay_vip,
    '5': download_free_movie,
    '6': download_pay_movie,
    '7': check_download_record,
    '8': check_notice
}

def user_view():
    client = tcp_client.get_client()
    while True:
        print('''
        ======= 普通用户功能 ======= 
        - 1.注册
        - 2.登录
        - 3.查看视频
        - 4.充值会员
        - 5.下载免费视频
        - 6.下载收费视频
        - 7.查看下载记录
        - 8.查看公告
        ''')

        choice = input('请输入功能编号:').strip()

        if choice == 'q':
            break

        if choice not in func_dic:
            continue

        func_dic.get(choice)(client)

#common

import json
import struct
import os
from conf import settings
import hashlib

def send_and_back(send_dic, client, file=None):
    json_data = json.dumps(send_dic).encode('utf-8')
    headers = struct.pack('i', len(json_data))

    client.send(headers)

    client.send(json_data)

    if file:
        with open(file, 'rb') as f:
            for line in f:
                client.send(line)

    headers = client.recv(4)
    data_len = struct.unpack('i', headers)[0]
    json_data = client.recv(data_len).decode('utf-8')
    back_dic = json.loads(json_data)

    return back_dic

def get_movie_list():
    movie_list = os.listdir(settings.UPLOAD_MOVIE_PATH)
    return movie_list

def get_movie_md5(movie_path):

    md5 = hashlib.md5()
    movie_size = os.path.getsize(movie_path)
    bytes_list = [0, movie_size // 4, movie_size // 2, movie_size - 10]

    with open(movie_path, 'rb') as f:
        for line in bytes_list:
            f.seek(line)
            data = f.read(10)
            md5.update(data)
    return md5.hexdigest()

#client

# coding=utf-8
import socket
from conf import settings
def get_client():

    client = socket.socket()

    client.connect(
        (settings.ip,settings.port)
    )

    return client

start

import os
import sys
from core import src

sys.path.append(
    os.path.dirname(__file__)
)

if __name__ == '__main__':
    src.run()
上一篇:CentOS中使用Docker+nginx部署Vue打包的dist项目


下一篇:数据采集与融合技术_实验3