客户端与服务端多功能传输练手小程序
客户端与服务端多功能传输小程序
server.py
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(('192.168.11.206',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
def run(conn):
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
print('服务端已经启动')
p_1 = ThreadPoolExecutor(2)
p_2 = ThreadPoolExecutor(2)
p_3 = ThreadPoolExecutor(2)
p_4 = ThreadPoolExecutor(2)
print('线程池准备完毕')
while True:
conn,adder = server.accept()
p_4.submit(run,conn)
client.py
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(('192.168.11.206',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()