def show_movies(): request_data = {'type': 'admin', 'method': 'get_movies'} response = TCPclient.request(request_data) for m in response['movies']: print('序号:%s 电影名:%s 件大小:%s'%(m['id'],m['name'],m['size'])) return response
def show_notice(): request_data={'type':'user', 'method':'show_notice' } response=TCPclient.request(request_data) for n in response['notices']: print('========%s========'%n['title']) print(n['content']) print('========%s========'%n['send_time'])
def download_movie(): response=show_movies() if not response: return ids=[m['id'] for m in response['movies']] id=input('请输入你要下载的电影序号>>>:') if id=='q': return if not id.isdigit(): print('输入不正确') return id=int(id) if id not in ids: print('输入不正确') return #接收文件,取出文件信息 movie=None for m in response['movies']: if m['id']==id: movie=m if not os.path.exists(settings.MOVIE_PATH): os.mkdir(settings.MOVIE_PATH) #判断本地是否存在这个文件 path=os.path.join(settings.MOVIE_PATH,movie['name']) if os.path.exists(path): print('文件已经下载') return #判断是否可以下载 request_data={'type':'user','method':'check_download','movie_id':id,'user_id':user['id']} response=TCPclient.request(request_data) if response['status']!='ok': print(response['msg']) return #添加属性,调用服务端download_movie下载 request_data['method']='download_movie' json_bytes = json.dumps(request_data).encode('utf-8') # 获取数据长度 len_bytes = struct.pack('i', len(json_bytes)) TCPclient.conn.send(len_bytes) TCPclient.conn.send(json_bytes) name=movie['name'] size=movie['size'] receive_size=0 f=open(path,'wb') while receive_size<size: if size-receive_size<1024: data=TCPclient.conn.recv(size-receive_size) else: data=TCPclient.conn.recv(1024) receive_size+=len(data) print('下载了%s%s' % ((receive_size / size) * 100, '%')) f.write(data) f.close() print('下载完成')
def open_vip(): if user['isvip']==1: print('你已经是会员了!') return res=(input('请支付100元开通vip会员,是否确认Y,输入其他取消')).lower() if res=='y': request_data={'type':'user', 'method':'open_vip', 'user_id':user['id']} response=TCPclient.request(request_data) print(response['msg']) else: print('操作错误')
def lock_user(): request_data={'type':'admin', 'method':'get_all_user'} response=TCPclient.request(request_data) for u in response['users']: print('%s %s %s'%(u['id'],u['name'],u['locked'])) id=int(input('请选择要锁定账户编号>>>:')) if id in [u['id'] for u in response['users']]: #根据id获取用户 for u in response['users']: if u['id']==id: #判断这个用户的锁定状态 if u['locked']==1: print('该账户已经锁定') break else: response_data={'type':'admin','method':'lock_user','user_id':id} response=TCPclient.request(response_data) print(response) else: print('输入id不正确')
def show_record(): request_data={ 'type':'user', 'method':'show_record', 'user_id':user['id'] } response=TCPclient.request(request_data) if not response: print('还没有下载记录') return # print(response) # {'records': [{'id': 1, 'user_id': 3, 'movie_id': 1, 'view_time': '2018-12-09 18:34:25'}]} for n in response['records']: for m in response['movies']: print('电影名称:%s ,下载时间:%s'%(m['name'],n['view_time']))
def delete_movie(): request_data={'type':'admin','method':'get_movies'} response=TCPclient.request(request_data) if not response['movies']: print('没有任何视屏') return ids=[d['id'] for d in response['movies']] for m in response['movies']: print('id:%s name:%s'%(m['id'],m['name'])) while True: id=int(input('请输入要删除的ID>>>:').strip()) if id in ids: reqyest_data={'type':'admin', 'method':'delete_movie', 'id':id} response=TCPclient.request(request_data) if response['status']=='ok': print('删除成功') return else: print(response['msg']) return else: print('id输入错误')
def send_notice(): while True: title=input('请输入标题>>>:') if title=='q': return if not title: print('标题不能为空') continue content=input('请输入内容>>>:') if not content: print('内容不能为空') continue request_data={'title':title, 'content':content, 'user_id':user['id'], 'type':'admin', 'method':'send_notice'} dic=TCPclient.request(request_data) print(dic['msg']) return
def register(user_type=0): while True: name = input('请输入注册用户名>>>:') if name == 'q': break password = input('请输入注册密码>>>:') if name and password: data = { 'name': name, 'password': password, 'type': 'common', 'user_type': user_type, 'method': 'register' } response = TCPclient.request(data) if response['status'] == 'ok': print('注册成功') break else: print(response['msg']) else: print('输入错误')
def login(user_type=0): while True: name = input('请输入登入用户名>>>:') if name == 'q': break password = input('请输入登入密码>>>:') if name and password: data = { 'name': name, 'password': password, 'type': 'common', #判断调用哪个接口模块 'user_type': user_type, #判断是管理员还是用户 'method': 'login' } response = TCPclient.request(data) if response['status'] == 'ok': print('登入成功') return response else: print(response['msg']) else: print('输入错误')
from UDPbroadcaster import UDPbroadcaster from UDPlistener import UDPlistener from TCPserver import TCPserver from TCPclient import TCPclient decision = input("1.want to start a chat\n" "2.wait for a request\n") ip = input("enter your ip address\n") if decision == "1": four = ip.split(".") subnet = four[0] + "." + four[1] + "." + four[2] + ".255" broadcaster = UDPbroadcaster() addrport = broadcaster.broadcast(subnet) tcp = TCPclient() print("enter 'exit' to leave the chat\n") tcp.startChat(addrport) if decision == "2": listener = UDPlistener() port = listener.listen(ip) tcp = TCPserver() print("enter 'exit' to leave the chat\n") tcp.startChat(port)
def upload_movie(): while True: # filepath=input('请输入上传文件路径>>>:').strip() filepath = r'D:\上海python全栈4期\day53\视频\1.youku系统回顾.mp4' if not os.path.exists(filepath): print('对不起:路径不存在!') continue if not os.path.isfile(filepath): print('对不起:只能上传文件') continue suffixs=['mp4','mkv','mov','rmvb','avi'] if not filepath.split('.')[-1] in suffixs: print('文件格式不支持!') continue md5=get_MD5(filepath) request_data = {'type': 'admin', 'method': 'check_movie','MD5':md5, } response=TCPclient.request(request_data) break if response['status']=='ok': print(response['msg']) #文件名 name=(os.path.split(filepath)[-1]) #文件后缀名 size=os.path.getsize(filepath) vip=input('请输入该视频是否收费,y收费,其他免费').strip().lower() if vip=='y': vip=1 else: vip=0 print('开始上传文件:%s'%name) #先发送文件信息 file_info={'name':name, 'size':size, 'MD5':md5, 'user_id':user['id'], 'isvip':vip, 'type':'admin', 'method':'upload_movie'} # 字典转json再转二进制 json_bytes = json.dumps(file_info).encode('utf-8') # 获取数据长度 len_bytes = struct.pack('i', len(json_bytes)) TCPclient.conn.send(len_bytes) TCPclient.conn.send(json_bytes) #上传文件 f=open(filepath,'rb') send_size=0 while send_size<size: data=f.read(1024) TCPclient.conn.send(data) send_size+=len(data) print('上传了%s%s'%((send_size/size)*100,'%')) print('上传完毕') #接受上传结果 len_data = TCPclient.conn.recv(4) len_bytes = struct.unpack('i', len_data)[0] json_data = TCPclient.conn.recv(len_bytes).decode('utf-8') dic = json.loads(json_data) print(dic['msg']) else: print(response['msg'])
from TCPclient import TCPclient client1 = TCPclient() client2 = TCPclient() x = 'a' # unreachable value while x != 0: # while the input is not 0, keep prompting the user which client he would like to choose if x == 1: # client1 client1.send('Chris', 'message.txt' ) # client1 will send to 'Chris' the file 'message.txt' client2.receive( ) # client2 will receive the file from 'Chris' from port 16000 elif x == 2: # the opposite for client 2 client2.send('Chris', 'message.txt') client1.receive() x = int(input('Choose which user is the sender (0 to exit): '))