day7 ftp
2016-03-15 14:04 dribs 阅读(213) 评论(0) 收藏 举报
readme
运行环境 Centos py3 s.py为服务端的程序 c.py为客户端的程序 为了达到测试上传下载效果,最好把两个文本文件放在不同的目录下 实现的功能: 1:登录 2:加密认证 3:上传下载 put|a.txt get|b.txt 4:ls 查看 5:q 退出 用户名:usera 密码:123 存在的BUG 登录失败后需要重新在运行服务端文件 无法上传下载绝对路径的文件 =====================实验效果=========================================== =============客户端:=============== [root@localhost ~]# python3 c.py username:usera passwd:123 cmd(put|a.txt):ls drwxr-xr-x. 18 root root 3.9K Mar 12 00:31 dev drwxr-xr-x. 102 root root 12K Mar 12 00:59 etc -rw-r--r--. 1 root root 0 Mar 12 01:52 flie drwxr-xr-x. 3 root root 4.0K Mar 12 00:28 home dr-xr-xr-x. 10 root root 4.0K Mar 12 00:22 lib dr-xr-xr-x. 9 root root 12K Mar 12 00:23 lib64 drwx------. 2 root root 16K Mar 12 00:16 lost+found dr-xr-xr-x. 7 li li 4.0K Mar 5 2013 media drwxr-xr-x. 3 root root 4.0K Mar 12 00:30 mnt cmd(put|a.txt):get|a.txt cmd(put|a.txt):put|a.txt aaaaaaaaa put user_input_split[1] a.txt 正在上传..... cmd(put|a.txt):exit Bye! [root@localhost ~]# ==========服务端显示的效果====== [root@localhost /]# python3 s.py nam usera passw 202cb962ac59075b964b07152d234b70 ftpserver is running.... ftpserver is running.... filename b'a.txt'
Ftpserver
1 #!/usr/bin/env python 2 # -*- encoding:utf-8 -*- 3 4 import socket 5 import subprocess 6 import time 7 import hashlib 8 9 userinfo= { 10 "usera":['123','/home/usera'], 11 "userb":['123','/home/userb'] 12 } 13 14 ip_port = ("127.0.0.1",9998) 15 sk = socket.socket() 16 sk.bind(ip_port) 17 sk.listen(5) 18 19 20 class FtpServer(object): 21 def __init__(self,ip,port): 22 self.ip = ip 23 self.port = port 24 def ls(self): 25 cmd = 'ls -lh' 26 cmd_call = subprocess.Popen(cmd,shell = True, stdout = subprocess.PIPE) 27 cmd_result = cmd_call.stdout.read() 28 conn.send(cmd_result) 29 def help(self): 30 helpmes = ( 31 ''' 32 put: put file ex:put|a.txt 33 get: get file ex:get|a.txt 34 ls :ls file ex:ls 35 q :exit 36 ''' 37 ) 38 conn.send(bytes(helpmes,'utf8')) 39 def get(self): 40 get_file_name = conn.recv(500) 41 a = str(get_file_name) 42 get_file_split = list(a.split("'")) 43 ff = open(get_file_split[1],'r') 44 for i in ff: 45 conn.send(bytes(i,'utf8')) 46 time.sleep(1) 47 conn.send(bytes("SERVER_SEND_OVER",'utf8')) 48 def put(self): 49 filename = conn.recv(500) 50 print("filename",filename) 51 f = open(filename,'a') 52 while True: 53 clientstr = conn.recv(1024) 54 if clientstr == b"FILE_TRANSFER_OVER": 55 break 56 f.write(clientstr.decode()) 57 print("ClientInputCmd is",clientstr) 58 if not clientstr:break 59 # f.write(clientstr.decode()) 60 f.close() 61 def exit(self): 62 conn.send(bytes("Bye!",'utf8')) 63 print("----client connect over--------") 64 def user(self): 65 pass 66 67 def uu(): 68 while True: 69 conn,addr = sk.accept() 70 ss = FtpServer('127.0.0.1',9998) 71 loop = True 72 while loop: 73 conn.send(bytes("username:",'utf8')) 74 nam = conn.recv(1024) 75 nam = nam.decode() 76 print("nam",nam) 77 if nam in userinfo: 78 conn.send(bytes("passwd:",'utf8')) 79 passw = conn.recv(1024) 80 passw = passw.decode() 81 print("passw",passw) 82 sp = '123' 83 hsp = hashlib.md5(sp.encode(encoding='utf8')) 84 hspassw = hsp.hexdigest() 85 if passw == hspassw: 86 loop = False 87 else: 88 print("passwd error") 89 else: 90 print("user error") 91 break 92 93 if __name__ == "__main__": 94 while True: 95 print("ftpserver is running....") 96 uu() 97 while True: 98 print("ftpserver is running....") 99 conn,addr = sk.accept() 100 ss = FtpServer('127.0.0.1',9998) 101 102 client_cmd = conn.recv(1024) 103 if hasattr(ss,client_cmd.decode()): 104 func = getattr(ss,client_cmd.decode()) 105 func() 106 endbye = client_cmd.decode() 107 sb = "byebye" 108 if endbye == sb: 109 break 110 conn.close()
FtpClient
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 import json 4 import socket 5 import time 6 import hashlib 7 import getpass 8 9 ip_port = ('127.0.0.1',9998) 10 11 class FtpClient(object): 12 def __init__(self,ip,port): 13 self.ip = ip 14 self.port = port 15 def ls(self): 16 sk.send(bytes(user_input_split[0],'utf8')) 17 servermessage = sk.recv(2048) 18 print(servermessage.decode()) 19 def help(self): 20 sk.send(bytes(user_input,'utf8')) 21 helpmsg = sk.recv(1024) 22 print(helpmsg.decode()) 23 def get(self): 24 f4 = open(user_input_split[1],'a') 25 sk.send(bytes(user_input_split[0],'utf8')) 26 sk.send(bytes(user_input_split[1],'utf8')) 27 while True: 28 getfiledata = sk.recv(1024) 29 if getfiledata == b'SERVER_SEND_OVER': 30 break 31 f4.write(getfiledata.decode()) 32 def put(self): 33 sk.send(bytes(user_input_split[0],'utf8')) 34 time.sleep(1) 35 print('aaaaaaaaa',user_input_split[0]) 36 sk.send(bytes(user_input_split[1],'utf8')) 37 print("user_input_split[1]",user_input_split[1]) 38 print("正在上传.....") 39 time.sleep(1) 40 f = open(user_input_split[1],'r') 41 for i in f: 42 sk.send(bytes(i,'utf8')) 43 time.sleep(1) 44 sk.send(bytes("FILE_TRANSFER_OVER",'utf8')) 45 f.close() 46 def exit(self): 47 sk.send(bytes(user_input_split[0],'utf8')) 48 bye = sk.recv(50) 49 print(bye.decode()) 50 quit() 51 def user(slef): 52 name = input("name:") 53 sendname = sk.send(bytes(name,'utf8')) 54 passwd = input("passwd:") 55 sendpasswd = sk.send(bytes(passwd,'utf8')) 56 57 def uu(): 58 while True: 59 sk = socket.socket() 60 sk.connect(ip_port) 61 sss = FtpClient('127.0.0.1',9998) 62 sname = sk.recv(1024) 63 cname = input(sname.decode()) 64 sk.send(bytes(cname,'utf8')) 65 spassw = sk.recv(1024) 66 i = spassw.decode() 67 cpassw = input(i) 68 p = hashlib.md5(cpassw.encode(encoding='utf8')) 69 passw = p.hexdigest() 70 sk.send(bytes(passw,'utf8')) 71 break 72 73 uu() 74 75 print( 76 ''' 77 help: 帮助 78 ls : 查看目录下的内容 79 get : 下载服务器的文件 get|b.txt 80 put : 上传文件到服务器 put|a.txt 81 q : 退出 82 ''' 83 ) 84 while True: 85 sk = socket.socket() 86 sk.connect(ip_port) 87 sss = FtpClient('127.0.0.1',9998) 88 89 user_input = input("cmd:").strip() 90 user_input_split = list(user_input.split("|")) 91 if len(user_input) == 0:continue 92 if user_input == "q": 93 sk.send(bytes("byebye",'utf8')) 94 quit() 95 if hasattr(sss,user_input_split[0]): 96 func = getattr(sss,user_input_split[0]) 97 func() 98 sk.close()
浙公网安备 33010602011771号