import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) class client_ftp(object): #客户端类 def __init__(self,add,port): #客户端属性,地址和端口 self.add = add self.port = port #注册用户 def register(self): username = input('请输入用户名:').strip() password = input('请输入密码:').strip() if not username or not password: print('输入为空!') elif username in os.listdir('%s/db'%Basedb): print('用户已存在!') else: save = {'username':username,'password':password} db_path = Basedb + '/db/' + username #存入用户信息 pickle.dump(save,open(db_path,'wb')) #生成用户文件夹 os.mkdir(Basedb + '/client_file/' + username) print('注册成功!') def login_in(self): #登陆验证方法 username = input('请输入用户名:').strip() password = input('请输入密码:').strip() if username in os.listdir('%s/db'%Basedb): db_path = Basedb + '/db/' + username db_dict = pickle.load(open(db_path,'rb')) if password == db_dict['password']: print('登陆成功!') flag = True else: print('密码错误!') flag = False else: print('用户名错误!') flag = False return flag def conn_server(self): #连接方法 client = socket.socket() client.connect((self.add,self.port)) code_in = input('请输入用户名:').strip() client.send(code_in.encode('UTF-8')) data = client.recv(1024000).decode() print(data) #如果返回成功 if data == 'success': server_file = os.listdir(Basedb + '/client_file/' + code_in) send_file = os.listdir(Basedb + '/send') while 1: option = input(''' -----option----- 1.输入1获取文件目录; 2.直接输入文件名称下载文件 3.将文件拖至send文件夹中,并输入文件名,上传到服务器 4.退出 ''').strip() if option == '1': client.send('check'.encode('utf-8')) print(client.recv(1024000).decode()) elif option in server_file: client.send(option.encode('utf-8')) data1 = client.recv(1024000).decode() file_name = Basedb + '/send' time_now =time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time())) with open('%s/%s'%(file_name,time_now),'w') as f: f.write(data1) print('接收成功!') elif option in send_file: client.send('load'.encode('utf-8')) print(client.recv(1024000).decode()) f_open = open('%s/send/%s'%(Basedb,option),'r') back_data = f_open.read() client.send(str(back_data).encode('utf-8')) f_open.close() print('传输成功!') else: exit() if __name__ == '__main__': client_start = client_ftp('localhost',6969) while 1: option = input(''' --------option-------- 1.注册 2.登陆 3.退出 ''').strip() if option == '1': client_start.register() elif option =='2': flag = client_start.login_in() if flag== True : client_start.conn_server() else: print('登陆失败了哥们') break else: exit()
import socket import os import time Base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) class Server_ftp(object): #此类为ftp服务端 def __init__(self,add,port): self.add = add self.port = port def conn_listen(self): server = socket.socket() server.bind((self.add,self.port)) server.listen(5) print('监听正在进行!监听地址为:',self.add,'监听端口为:',self.port) while 1: conn,addr = server.accept() print('已建立连接!') data = conn.recv(1024000).decode() print("recv:", data) if not data: print("client has lost...") break elif os.path.exists(Base_path + '/client_file/' + data): allfile = os.listdir(Base_path + '/client_file/' + data) conn.send('success'.encode('utf-8')) while 1: data1 = conn.recv(1024000).decode() if not data1: print("client has lost...") break elif data1 == 'check': conn.send(str(allfile).encode('utf-8')) break elif data1 in allfile: file_name = Base_path + '/client_file/' + data f_open = open('%s/%s'%(file_name,data1),'rb') back_data = f_open.read() conn.send(str(back_data).encode('utf-8')) f_open.close() break elif data1 == 'load': conn.send('reading'.encode('utf-8')) data2 = conn.recv(1024000).decode() if not data2: print("client has lost...") break else: file_name = Base_path + '/client_file/' + data time_now =time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time())) with open('%s/%s'%(file_name,time_now),'w') as f: f.write(data2) print('接收成功!') conn.send('接收成功!'.encode('utf-8')) break else: print('data1输入错误!') conn.send('data1输入错误!'.encode('utf-8')) else: print('接收错误!') conn.send('错误输入!'.encode('utf-8')) break if __name__ == '__main__': sever_start = Server_ftp('localhost',6969) sever_start.conn_listen()