网络编程小练习1--登录上传,下载功能,类型网盘功能
# 1.登录 + 文件下载
# 用户必须登录才能下载
# 用户是否登录应该记录在服务器
# 并且用户可以自己选择 上传 还是 下载
server端 # _*_ coding : UTF-8 _*_ import socket import json import struct import os dic_key = {'alex':'123'} dir_name = os.path.dirname(__file__) #返回path的目录 E:/untitled3/test作业/day30/上传下载 显示当前路径 dowload_path = os.path.join(dir_name,'服务端的下载目录') #将多个路径组合后返回E:/untitled3/test作业/day30/上传下载\服务端的下载目录 dowload_file = os.listdir(dowload_path) #列出指定目录下的所有文件和子目录,以列表方式 获取下载的目录文件列表 # print(dowload_file) #['下载测试文件1.txt', '下载测试文件2.txt'] sk=socket.socket() sk.bind(('127.0.0.1',7001)) sk.listen() conn,addr = sk.accept() res = conn.recv(1024).decode('utf-8') #接收用户名,密码的字符串 recv_dic = json.loads(res) #转换 if recv_dic['username'] in dic_key and recv_dic['password'] == dic_key[recv_dic['username']]: print(f'{recv_dic["username"]}登录成功') conn.send(b'0') #给客户端发送回执 new_content = conn.recv(1024).decode('utf-8') if new_content == '1': lens =conn.recv(4) length =struct.unpack('i',lens)[0] f_fun =conn.recv(length).decode('utf-8') file_dic = json.loads(f_fun) with open(file_dic['filename'],mode='wb') as f: if file_dic['filesize'] > 0: content =conn.recv(1024) file_dic['filesize'] -= len(content) f.write(content) conn.close() elif new_content == '2': seng_filelist=json.dumps(dowload_file).encode('utf-8') conn.send(seng_filelist) down_file = conn.recv(1024).decode('utf-8') #接收要下载的文件名 down_path = os.path.join(dowload_path,down_file) #下载路径拼接 取得文件 down_size = os.path.getsize(down_path) #取得文件大小 # dic = {'filename':down_path,'filesize':down_size} # new_dic = json.dumps(dic) # conn.send(new_dic.encode('utf-8')) conn.send(str(down_size).encode('utf-8')) #这样可以不用json转成字符串 with open(down_path,mode='rb') as f: while down_size >0: con = f.read(1024) down_size -= len(con) conn.send(con) else: print('登录失败') conn.close() sk.close()
client端 # _*_ coding : UTF-8 _*_ import socket import json import struct import os num = ['上传','下载'] sk= socket.socket() sk.connect(('127.0.0.1',7001)) username = input('请输入用户名:').strip() password = input('请输入密码:').strip() dic = {'username':username,'password':password} #用户名,密码 srt_dic = json.dumps(dic) #转成字符串 sk.send(srt_dic.encode('utf-8')) #发送 login_mag = sk.recv(1024).decode('utf-8') if login_mag == '0': print('登录成功') for i,v in enumerate(num,1): print(i,v) no = input('请输入功能序号').strip() if no == '1': #上传 sk.send(b'1') abs_path = r'E:\msdia80.dll' filename = os.path.basename(abs_path) #获得文件名 filesize = os.path.getsize(abs_path) #文件大小 dic = {'filename':filename,'filesize':filesize} new_dic=json.dumps(dic).encode('utf-8') s_dic = struct.pack('i',len(new_dic)) sk.send(s_dic) sk.send(new_dic) with open(abs_path,mode='rb') as f: if filesize>0: content=f.read(1024) filesize -= len(content) sk.send(content) sk.close() elif no == '2':#下载 sk.send(b'2') ret = sk.recv(1024).decode('utf-8') down_list = json.loads(ret) while True: for v ,i in enumerate(down_list,1): print(v,i) no = input('请输入序号选择下载的文件:').strip() if no.isdecimal() and 0 < int(no) <= len(down_list): down_file = down_list[int(no)-1].encode('utf-8') #选择的文件名 sk.send(down_file) down_size = sk.recv(1024).decode('utf-8') #接收下载文件的大小 size = int(down_size) #服务端用str转成字符串,客户端用int转成大小 with open(down_file,mode='wb') as f: while size > 0: con = sk.recv(1024) size -= len(con) f.write(con) sk.close() break else: print('选择的序号有误,请重新输入') continue
2.用socoketservet模块
server端 # _*_ coding : UTF-8 _*_ import socket import json import struct import os import socketserver dic_key = {'zeng':'123'} dir_name = os.path.dirname(__file__) #返回path的目录 E:/untitled3/test作业/day30/上传下载 显示当前路径 dowload_path = os.path.join(dir_name,'服务端的下载目录') #将多个路径组合后返回E:/untitled3/test作业/day30/上传下载\服务端的下载目录 dowload_file = os.listdir(dowload_path) #列出指定目录下的所有文件和子目录,以列表方式 获取下载的目录文件列表 # print(dowload_file) #['下载测试文件1.txt', '下载测试文件2.txt'] class Myserver(socketserver.BaseRequestHandler): def handle(self): conn = self.request # sk=socket.socket() # sk.bind(('127.0.0.1',7001)) # sk.listen() # conn,addr = sk.accept() res = conn.recv(1024).decode('utf-8') #接收用户名,密码的字符串 recv_dic = json.loads(res) #转换 if recv_dic['username'] in dic_key and recv_dic['password'] == dic_key[recv_dic['username']]: print(f'{recv_dic["username"]}登录成功') conn.send(b'0') #给客户端发送回执 new_content = conn.recv(1024).decode('utf-8') if new_content == '1': lens =conn.recv(4) length =struct.unpack('i',lens)[0] f_fun =conn.recv(length).decode('utf-8') file_dic = json.loads(f_fun) with open(file_dic['filename'],mode='wb') as f: if file_dic['filesize'] > 0: content =conn.recv(1024) file_dic['filesize'] -= len(content) f.write(content) conn.close() elif new_content == '2': seng_filelist=json.dumps(dowload_file).encode('utf-8') conn.send(seng_filelist) down_file = conn.recv(1024).decode('utf-8') #接收要下载的文件名 down_path = os.path.join(dowload_path,down_file) #下载路径拼接 取得文件 down_size = os.path.getsize(down_path) #取得文件大小 # dic = {'filename':down_path,'filesize':down_size} # new_dic = json.dumps(dic) # conn.send(new_dic.encode('utf-8')) conn.send(str(down_size).encode('utf-8')) #这样可以不用json转成字符串 with open(down_path,mode='rb') as f: while down_size >0: con = f.read(1024) down_size -= len(con) conn.send(con) else: print('登录失败') server = socketserver.ThreadingTCPServer(('127.0.0.1',7002),Myserver) server.serve_forever()
client 端 # _*_ coding : UTF-8 _*_ import socket import json import struct import os num = ['上传','下载'] sk= socket.socket() sk.connect(('127.0.0.1',7002)) username = input('请输入用户名:').strip() password = input('请输入密码:').strip() dic = {'username':username,'password':password} #用户名,密码 srt_dic = json.dumps(dic) #转成字符串 sk.send(srt_dic.encode('utf-8')) #发送 login_mag = sk.recv(1024).decode('utf-8') if login_mag == '0': print('登录成功') for i,v in enumerate(num,1): print(i,v) no = input('请输入功能序号').strip() if no == '1': #上传 sk.send(b'1') abs_path = r'E:\msdia80.dll' filename = os.path.basename(abs_path) #获得文件名 filesize = os.path.getsize(abs_path) #文件大小 dic = {'filename':filename,'filesize':filesize} new_dic=json.dumps(dic).encode('utf-8') s_dic = struct.pack('i',len(new_dic)) sk.send(s_dic) sk.send(new_dic) with open(abs_path,mode='rb') as f: if filesize>0: content=f.read(1024) filesize -= len(content) sk.send(content) sk.close() elif no == '2':#下载 sk.send(b'2') ret = sk.recv(1024).decode('utf-8') down_list = json.loads(ret) while True: for v ,i in enumerate(down_list,1): print(v,i) no = input('请输入序号选择下载的文件:').strip() if no.isdecimal() and 0 < int(no) <= len(down_list): down_file = down_list[int(no)-1].encode('utf-8') #选择的文件名 sk.send(down_file) down_size = sk.recv(1024).decode('utf-8') #接收下载文件的大小 size = int(down_size) #服务端用str转成字符串,客户端用int转成大小 with open(down_file,mode='wb') as f: while size > 0: con = sk.recv(1024) size -= len(con) f.write(con) sk.close() break else: print('选择的序号有误,请重新输入') continue