使用socket实现FTP程序
#-*- coding:utf-8 -*- import socketserver from module import * class server: def __init__(self,request): self.conn=request self.conn.sendall(by('欢迎光临大龙FTP!')) def login(self): self.user=st(self.conn.recv(1024)) self.conn.sendall(by('1')) self.password=st(self.conn.recv(1024)) if self.user in userdict.keys() and self.password==userdict[self.user]: self.result='Success' self.conn.sendall(by('\033[32m登陆成功!\033[0m')) else: self.result='Failed' self.conn.sendall(by('\033[31m登陆失败!\033[0m')) log(self.user,self.result,'users_log.txt') return self.result def register(self): self.user=st(self.conn.recv(1024)) self.conn.sendall(by('1')) self.password=st(self.conn.recv(1024)) if self.user in userdict.keys(): self.conn.sendall(by('\033[031m注册失败,该用户已存在\033[0m')) else: self.conn.sendall(by('\033[032m注册成功!\033[0m')) userdict[self.user]=self.password self.userdump() log(self.user,self.result,'users_log.txt') def put(self): self.use=0 self.name=st(self.conn.recv(1024)) self.conn.sendall(by('1')) self.size=int(st(self.conn.recv(1024))) if os.path.isfile('ftp\\'+self.name): print('cun zai') print('ftp\\'+self.name) self.have=os.path.getsize('ftp\\'+self.name) self.conn.sendall(by(str(self.have))) self.choose=st(self.conn.recv(1024)) if self.choose=='2': self.have=0 self.conn.sendall(by('1')) # self.size=int(st(self.conn.recv(1024))) self.conn.sendall(by('11')) f=open('ftp\\'+self.name,'ab') while self.size != self.use: self.line=self.conn.recv(1024) f.write(self.line) self.use+=len(self.line) f.close() print('wanbi') log(self.user,'Success','file_log.txt') if self.choose=='1': self.conn.sendall(by('1')) # self.size=int(st(self.conn.recv(1024))) self.conn.sendall(by('11')) f=open('ftp\\'+self.name,'ab') while self.size != self.use: self.line=self.conn.recv(1024) f.write(self.line) self.use+=len(self.line) f.close() print('wanbi') log(self.user,'Success','file_log.txt') else: print('no zai') self.conn.sendall(by(str(0))) # self.size=int(st(self.conn.recv(1024))) self.conn.sendall(by('11')) f=open('ftp\\'+self.name,'wb') while self.size > self.use: self.line=self.conn.recv(1024) f.write(self.line) self.use+=len(self.line) f.close() print('wanbi') log(self.user,'Success','file_log.txt') def get(self): self.cmd() self.path=st(self.conn.recv(1024)) if self.path not in os.listdir('ftp'): self.conn.sendall(by('0')) else: self.conn.sendall(by('1')) self.size=os.path.getsize(self.path) self.conn.sendall(by(str(self.size))) self.conn.recv(1024) with open('ftp\\%s'%self.path,'rb') as f : for line in f: self.conn.sendall(line) log(self.user,'Success','file_log.txt') def cmd(self): cmd=st(self.conn.recv(1024)) p = os.popen(cmd) x = p.read() self.conn.sendall(by(x)) @staticmethod def userdump(): with open('user.txt','wb') as f: pickle.dump(userdict,f) f.close() class Myserver(socketserver.BaseRequestHandler): def handle(self): s=server(self.request) while True: opt=st(s.conn.recv(1024)) print(userdict) if opt =='1': self.result=s.login() if 'Failed' in self.result:continue while True: opt2 = st(s.conn.recv(1024)) if opt2 =='1': print('put') s.put() elif opt2=='3': s.cmd() elif opt2=='2': print('get') s.get() elif opt2=='4': break elif opt=='2': s.register() elif opt=='3': break if __name__=='__main__': Server=socketserver.ThreadingTCPServer(('127.0.0.1',8888),Myserver) Server.serve_forever() # print(dir(socketserver))
#-*- coding:utf-8 -*- from module import * import os import socket import pickle import os import sys try: userdict = pickle.load(open('user.txt', 'rb')) except Exception as e: userdict = {} def log(user,result,file): with open(file,'a') as f: attime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) f.write('%s %s %s\n'%(attime,result,user)) class client: def __init__(self,address,port): self.obj=socket.socket() self.obj.connect((address,port)) print(st(self.obj.recv(1024))) def login(self,user,password): self.obj.sendall(by(user)) self.obj.recv(1024) self.obj.sendall(by(password)) self.result=st(self.obj.recv(1024)) print(self.result) def register(self,user,password): self.obj.sendall(by(user)) self.obj.recv(1024) self.obj.sendall(by(password)) self.result=st(self.obj.recv(1024)) print(self.result) def put(self,path): self.size=os.path.getsize(path) name=path.split('\\')[-1] self.obj.sendall(by(name)) self.obj.recv(10241) self.obj.sendall(by(str(self.size))) have=int(st(self.obj.recv(1024))) if have>=self.size: print('\033[031m该文件已存在\033[0m!') self.obj.sendall(by(str(0))) elif 0<have<self.size: choose=input('1、断点续传 2、重新传\n请选择:').strip() self.obj.sendall(by(choose)) if choose=='2':have=0 self.obj.sendall(by(str(self.size))) st(self.obj.recv(1024)) with open(path,'rb') as f : f.seek(have) for line in f: self.obj.sendall(line) have+=len(line) schedule(self.size,have) f.close() print('\033[032m上传成功!\033[0m') else: self.obj.sendall(by(str(self.size))) st(self.obj.recv(1024)) with open(path,'rb') as f : f.seek(have) for line in f: self.obj.sendall(line) have+=len(line) schedule(self.size,have) f.close() print('\033[032m上传成功!\033[0m') def get(self,path): # self.obj.sendall(by(path)) # self.have=st(self.obj.recv(1024)) # if self.have=='0': self.size=int(st(self.obj.recv(1024))) self.obj.sendall(by('1')) self.use=0 if os.path.isfile('get\\'+path): self.cover=input('\033[031m该文件已存在,是否覆盖?\n\t1、是\t\t2、否\n\033[0m请选择:').strip() if self.cover=='1': f=open('get\\'+path,'wb') while self.size != self.use: self.line=self.obj.recv(1024) f.write(self.line) self.use+=len(self.line) schedule(self.size,self.use) f.close() print('\033[032m下载成功\033[0m') elif self.cover=='2':pass else:print('\033[31m输入无效\033[0m') else: f=open('get\\'+path,'wb') while self.size> self.use: self.line=self.obj.recv(1024) f.write(self.line) self.use+=len(self.line) schedule(self.size,self.use) f.close() print('\033[032m下载成功\033[0m') # self.obj.recv() def cmd(self,cmd): self.obj.sendall(by(cmd)) self.result=st(self.obj.recv(4096)) print('\033[32m%s\033[0m'%self.result) c=client('127.0.0.1',8888) while True: opt=input('请选择: 1、登陆 2、注册 3、退出\n>>>') c.obj.sendall(by(opt)) if opt=='1': user = input('请输入用户名:') password = input('请输入密码:') c.login(user,password) if '失败' in c.result:continue while True: opt2 = input('请选择: 1、上传 2、下载 3、执行命令 4、退出\n>>>') if opt2=='1': path=input('请输入要上传的文件路径:') if not os.path.isfile(path): print('\033[031m输入路径无效!\033[0m') continue else: c.obj.sendall(by(opt2)) c.put(path) elif opt2=='2': c.obj.sendall(by(opt2)) c.cmd('dir ftp | findstr /v 目录 |findstr /v 驱动器 |findstr /v 序列号|findstr /v DIR') path=input('请输入要下载的文件名:').strip() c.obj.sendall(by(path)) have=st(c.obj.recv(1024)) if have=='0': print('\033[031m输入路径无效!\033[0m') continue else:c.get(path) elif opt2=='3': c.obj.sendall(by(opt2)) cmd=input('请输入要执行的命令:') c.cmd(cmd) elif opt2=='4':break else: print('\033[31m输入无效\033[0m ') continue elif opt=='2': user = input('请输入用户名:') password = input('请输入密码:') c.register(user,password) elif opt == '3': break else: print('\033[31m输入无效\033[0m') continue
#-*- coding:utf-8 -*- import socket import pickle import os import sys import time try: userdict = pickle.load(open('user.txt', 'rb')) except Exception as e: print(e) userdict = {} def log(user,result,file): with open(file,'a') as f: attime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) f.write('%s %s %s\n'%(attime,result,user)) def by(word): a=bytes(word,encoding = 'utf-8') return a def st(word): b=str(word,encoding = 'utf-8') return b def schedule(size,use): sys.stdout.write("\r") sys.stdout.write("%s%% | %s" % (int(use / size * 100), int(use / size * 100) * '#')) sys.stdout.flush()