[Terry Notes] python FTP, terrypython

Source: Internet
Author: User

[Terry Notes] python FTP, terrypython

The following is a job, using python as an ftp, mainly using socket.

The server runs in linux, and the client can execute shell commands (static)

Enter get xxx on the client side to download the file.

Input put xxx on the client side to upload the file.

 

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.