[terry筆記]python FTP,terrypython

來源:互聯網
上載者:User

[terry筆記]python FTP,terrypython

如下是作業,用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                     continue29                 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 = 035                 while True:36                     data2 = conn.recv(1024)37                     f.write(data2)38                     file_size += len(data2)39                     if file_size == total_size:40                         break41                 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             break52         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: continue11         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                 continue17             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 = 024                 while True:25                     data2 = client.recv(1024)26                     f.write(data2)27                     file_size += len(data2)28                     if file_size == total_size:29                         break30                 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 = 044             res = b""45             while received_size < total_size:46                 d = client.recv(1024)47                 res += d48                 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.