如下是作业,用python做一个ftp,主要利用socket。
server端在linux下运行,在client端可以执行shell命令(静态的)
在client端输入get xxx,即可下载。
在client端输入put xxx,即可上传。
server端:
1 import socket 2 import subprocess 3 import os 4 server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 5 server.bind(("0.0.0.0",8000)) 6 server.listen(5) 7 print("start to listen".center(30,"-")) 8 9 while True: 10 conn,client_addr = server.accept() 11 print(conn,client_addr) 12 while True: 13 try: 14 data = conn.recv(1024) 15 print("receive from client :",data) 16 if (data.decode()).startswith("get"): 17 data_cmd = data.decode() 18 data_cmd_list = data_cmd.split(" ") 19 file_name = data_cmd_list[-1] 20 conn.send(str(os.path.getsize(file_name)).encode()) 21 f = open(file_name,"rb") 22 f_data = f.readlines() 23 for line in f_data: 24 conn.send(line) 25 elif (data.decode()).startswith("put"): 26 data_size = conn.recv(1024) 27 if data_size.decode() == "no file": 28 continue 29 print("The file's size is %sM" % round(int(data_size.decode()) / 1024 / 1024, 2)) 30 data_list = (data.decode()).split(" ") 31 file_name = data_list[-1] 32 f = open(file_name, "wb") 33 total_size = int(data_size.decode()) 34 file_size = 0 35 while True: 36 data2 = conn.recv(1024) 37 f.write(data2) 38 file_size += len(data2) 39 if file_size == total_size: 40 break 41 f.close() 42 print("file upload done") 43 else: 44 res_obj = subprocess.Popen(data,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) 45 res = res_obj.stdout.read() 46 conn.send(str(len(res)).encode()) 47 print("--res len :",len(res)) 48 conn.send(res) 49 except ConnectionResetError as e: 50 print(client_addr,"is break") 51 break 52 except FileNotFoundError as e: 53 print("there is no such file!") 54 conn.send(b"no file") 55 continue
client端:
1 import socket 2 import os 3 client = socket.socket() 4 client.connect(("192.168.168.128",8000)) 5 # client.connect(("localhost",8000)) 6 7 while True: 8 try: 9 msg = input(">>>>>").strip() 10 if len(msg) == 0: continue 11 client.send(msg.encode()) 12 if msg.startswith("get"): 13 data = client.recv(1024) 14 if data.decode() == "no file": 15 print("there is no such file!") 16 continue 17 else: 18 print("The file's size is %sM" % round(int(data.decode())/1024/1024,2)) 19 msg_list = msg.split(" ") 20 file_name = msg_list[-1] 21 f = open(file_name,"wb") 22 total_size = int(data.decode()) 23 file_size = 0 24 while True: 25 data2 = client.recv(1024) 26 f.write(data2) 27 file_size += len(data2) 28 if file_size == total_size: 29 break 30 f.close() 31 elif msg.startswith("put"): 32 msg_cmd_list = msg.split(" ") 33 file_name = msg_cmd_list[-1] 34 client.send(str(os.path.getsize(file_name)).encode()) 35 f = open(file_name, "rb") 36 f_data = f.readlines() 37 for line in f_data: 38 client.send(line) 39 else: 40 data = client.recv(1024) 41 print("client receive:", data.decode()) 42 total_size = int(data.decode()) 43 received_size = 0 44 res = b"" 45 while received_size < total_size: 46 d = client.recv(1024) 47 res += d 48 received_size += len(d) 49 print("-----receive done-----") 50 print(res.decode()) 51 except FileNotFoundError as e: 52 print("There is no such file!") 53 client.send(b"no file") 54 continue