支持多用户在线的Ftp程序
Posted wangheng #这里是用户名
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了支持多用户在线的Ftp程序相关的知识,希望对你有一定的参考价值。
一、要求
二、思路
三、代码
服务器端:
# #!/usr/bin/env python # # -*- coding: utf-8 -*- import sys import time import socket import hashlib import pickle import subprocess import socketserver class Myserver(socketserver.BaseRequestHandler): def recv_file(self): conn=self.request file_size=int(str(conn.recv(1024),encoding=‘utf-8‘)) print(file_size) conn.sendall(bytes(‘welcome‘,encoding=‘utf-8‘)) has_recv=0 with open(‘new.png‘,‘wb‘) as f: while has_recv<=file_size: data=conn.recv(10240) f.write(data) has_recv+=len(data) def command(self): conn=self.request a=conn.recv(1024) ret=str(a,encoding=‘utf-8‘) ret2 = subprocess.check_output(ret, shell=True) conn.sendall(ret2) def md5(self,pwd): hash=hashlib.md5(bytes(‘xx7‘,encoding=‘utf-8‘)) hash.update(bytes(pwd,encoding=‘utf-8‘)) return hash.hexdigest() def login(self,usrname,pwd): conn=self.request s=pickle.load(open(‘name_pwd‘,‘rb‘)) if usrname in s: if s[usrname]==self.md5(pwd): #和加密后的密码进行比较 return True else: return False else: return False def regist(self,usrname,pwd): conn=self.request s=pickle.load(open(‘name_pwd‘,‘rb‘)) if usrname in s: return False else: s[usrname]=self.md5(pwd) pickle.dump(s,open(‘name_pwd‘,‘wb‘)) return True def before(self): conn=self.request b=conn.recv(1024) ret=str(b,encoding=‘utf-8‘) print(ret) conn.sendall(bytes(‘b ok‘,encoding=‘utf-8‘)) c=conn.recv(1024) r=str(c,encoding=‘utf-8‘) usrname,pwd=r.split(‘,‘) if ret==‘1‘: r=self.login(usrname,pwd) if r: conn.sendall(bytes(‘y‘,encoding=‘utf-8‘)) else: conn.sendall(bytes(‘n‘,encoding=‘utf-8‘)) elif ret==‘2‘: # print(usrname,pwd) r=self.regist(usrname,pwd) if r: conn.sendall(bytes(‘y‘,encoding=‘utf-8‘)) else: conn.sendall(bytes(‘n‘,encoding=‘utf-8‘)) def handle(self): conn=self.request conn.sendall(bytes(‘welcome‘,encoding=‘utf-8‘)) self.before() #登陆或注册验证 while True: a=conn.recv(1024) conn.sendall(bytes(‘收到a‘,encoding=‘utf-8‘)) ret=str(a,encoding=‘utf-8‘) if ret==‘1‘: self.recv_file() # conn.sendall(bytes(‘file ok‘,encoding=‘utf-8‘)) elif ret==‘2‘: self.command() elif ret==‘q‘: break else: pass if __name__==‘__main__‘: sever=socketserver.ThreadingTCPServer((‘127.0.0.1‘,9999),Myserver) sever.serve_forever()
客户端:
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import time import os import socket def send_file(file_path): size=os.stat(file_path).st_size obj.sendall(bytes(str(size),encoding=‘utf-8‘)) obj.recv(1024) has_send=0 with open(‘f.png‘,‘rb‘) as f: for line in f: obj.sendall(line) has_send+=len(line) sys.stdout.write(‘\r‘) #清空文件内容 time.sleep(0.2) sys.stdout.write(‘已发送%s%%|%s‘ %(int(has_send/size*100),(round(has_send/size*40)*‘★‘))) sys.stdout.flush() #强制刷出内存 print(‘\n‘) def command(command_name): obj.sendall(bytes(command_name,encoding=‘utf-8‘)) ret=obj.recv(10240) #等待客户端发送 r=str(ret,encoding=‘GBK‘) print(r) def login(usrname,pwd): obj.sendall(bytes(usrname+‘,‘+pwd,encoding=‘utf-8‘)) ret=obj.recv(1024) r=str(ret,encoding=‘utf-8‘) if r==‘y‘: return 1 else: return 0 def regist(usrname,pwd): obj.sendall(bytes(usrname+‘,‘+pwd,encoding=‘utf-8‘)) ret=obj.recv(1024) r=str(ret,encoding=‘utf-8‘) if r==‘y‘: return 1 else: return 0 def before(): usrname=input(‘请输入用户名‘) pwd=input(‘请输入密码‘) a=input(‘请选择1.登陆 2.注册‘) obj.sendall(bytes(a,encoding=‘utf-8‘)) obj.recv(1024) if a==‘1‘: ret=login(usrname,pwd) if ret: print(‘登陆成功‘) return 1 else: print(‘用户名或密码错误‘) return 0 elif a==‘2‘: ret=regist(usrname,pwd) if ret: print(‘注册成功‘) return 1 else: print(‘用户名已存在‘) return 0 obj=socket.socket() obj.connect((‘127.0.0.1‘,9999)) ret=obj.recv(1024) #等待客户端发送 r=str(ret,encoding=‘utf-8‘) print(r) result=before()#登陆或注册 if result: while True: a=input(‘请选择1.传文件 2.执行命令 q退出:‘) obj.sendall(bytes(str(a),encoding=‘utf-8‘)) ret=obj.recv(1024) #确认是否收到a r=str(ret,encoding=‘utf-8‘) print(r) if a==‘1‘: # b=input(‘请输入文件路径:‘) b=‘f.png‘ if os.path.exists(b): send_file(b) obj.sendall(bytes(‘hhe‘,encoding=‘utf-8‘)) # obj.recv(1024) elif a==‘2‘: b=input(‘请输入command:‘) command(b) elif a==‘q‘: break else: print(‘输入错误‘) obj.close()
以上是关于支持多用户在线的Ftp程序的主要内容,如果未能解决你的问题,请参考以下文章