server.pypython
from threading import Thread,Event,Lock from concurrent.futures import ThreadPoolExecutor from socket import socket from struct import pack,unpack from json import * import os PATH = os.path.dirname(os.path.abspath(__file__)) SERVERPATH = os.path.join(PATH,'server') CLIENTPATH = os.path.join(PATH,'client') if not os.path.exists(SERVERPATH):os.mkdir(SERVERPATH) if not os.path.exists(CLIENTPATH):os.mkdir(CLIENTPATH) server = socket() server.bind(('127.0.0.1',8000)) server.listen(5) def register(conn): print('客戶端鏈接到了註冊程序') while True: data = loads(conn.recv(1024).decode('utf8')) file_path = os.path.join(PATH, f'{data[0]}.json') if os.path.exists(file_path): print('客戶端註冊的文件已經存在') conn.send('客戶端註冊的文件已經存在'.encode('utf8')) continue else: with open(file_path, 'w', encoding='utf8') as fw: dump(data, fw) print('客戶端註冊成功') conn.send('客戶端註冊成功'.encode('utf8')) break def login(conn): print('客戶端鏈接到了登入程序') while True: data = loads(conn.recv(1024).decode('utf8')) file_path = os.path.join(PATH, f'{data[0]}.json') if not os.path.exists(file_path): print('客戶端帳號不存在') conn.send('客戶端帳號不存在'.encode('utf8')) continue else: with open(file_path, 'r', encoding='utf8') as fr: if data == load(fr): print('客戶端登入成功') conn.send('客戶端登入成功'.encode('utf8')) break else: conn.send('客戶端帳號密碼錯誤'.encode('utf8')) continue def download(): print('客戶端鏈接到了下載程序') while True: cmd = conn.recv(1024).decode('utf8') if cmd == 'download': conn.send(dumps((os.listdir(SERVERPATH))).encode('utf8')) flie_name_size = conn.recv(1024).decode('utf8') if flie_name_size == '服務端沒有文件': break file_name = loads(flie_name_size)[0] file_size = loads(flie_name_size)[1] file_path = os.path.join(SERVERPATH, file_name) print(file_path) with open(file_path, 'rb') as fr: fr.seek(file_size) data = fr.read() print(data) conn.send(pack('i', len(data))) conn.send(data) print('服務端文件發送完畢') break print('服務端已經啓動') p_1 = ThreadPoolExecutor(5) p_2 = ThreadPoolExecutor(5) p_3 = ThreadPoolExecutor(5) print('進程池準備完畢') while True: conn,adder = server.accept() while True: try: print('等待客戶端選擇功能') action = conn.recv(1024).decode('utf8') if action == 'register': res = p_1.submit(register,conn) res.result() elif action == 'login': res =p_2.submit(login,conn) res.result() elif action == 'download': res =p_3.submit(download,conn) res.result() elif action == 'out':break except ConnectionResetError: print('客戶端已經斷開') break
client.py
ios
from threading import Thread,Event,Lock from socket import socket from struct import pack,unpack from json import loads,dumps import os PATH = os.path.dirname(os.path.abspath(__file__)) SERVERPATH = os.path.join(PATH,'server') CLIENTPATH = os.path.join(PATH,'client') if not os.path.exists(SERVERPATH):os.mkdir(SERVERPATH) if not os.path.exists(CLIENTPATH):os.mkdir(CLIENTPATH) client = socket() client.connect(('127.0.0.1',8000)) def register(): print('歡迎使用註冊功能') while True: name = input('請輸入你的名字') pwd = input('請輸入你的密碼') client.send(dumps((name, pwd)).encode('utf8')) data = client.recv(1024).decode('utf8') print(data) if data == '客戶端註冊成功': print('註冊成功') break else: print('帳戶已經存在') def login(): while True: name = input('請輸入你的名字') pwd = input('請輸入你的密碼') client.send(dumps((name, pwd)).encode('utf8')) data = client.recv(1024).decode('utf8') if data =='客戶端登入成功': print('登入成功') break else: print('登入失敗') def download(): print('歡迎使用註冊功能') while True: client.send('download'.encode('utf8')) file_lis = loads(client.recv(1024).decode('utf8')) for a,b in enumerate(file_lis): print(a,b) if file_lis ==[]: client.send('服務端沒有文件'.encode('utf8')) print('服務端沒有文件') break while True: file_index = int(input('請選擇你要下載文件的序號')) try: file_name = file_lis[file_index] break except: print('好好輸入') continue file_name = file_lis[file_index] file_path = os.path.join(CLIENTPATH,file_name) try: with open(file_path,'rb') as fr: file_size = len(fr.read()) except: file_size = 0 client.send(dumps((file_name,file_size)).encode('utf8')) size = unpack('i',client.recv(4))[0] print(size) while file_size != size: data = client.recv(1024) with open(file_path,'ab') as fa: fa.write(data) with open(file_path, 'rb') as fr: file_size = len(fr.read()) print(file_size) print('下載完成') break def run(): while True: msg = ''' 輸入0 註冊功能 輸入1 登入功能 輸入2 下載功能 輸入Q 退出 ''' print(msg) chiose = input('請選擇你要要運行的程序') if chiose == 'Q':client.send('out'.encode('utf8'));print('程序退出');break elif chiose == '0':client.send('register'.encode('utf8'));register() elif chiose == '1':client.send('login'.encode('utf8'));login() elif chiose == '2':client.send('download'.encode('utf8'));download() else:print('好好輸入');continue if __name__ == '__main__': run()