Here is the job, using Python to do an FTP, the main use of sockets.
Server side runs under Linux and can execute shell commands (static) on client side
You can download it by entering get XXX on the client side.
Enter put XXX on the client side to upload.
Server side:
1 ImportSocket2 Importsubprocess3 ImportOS4Server =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)5Server.bind (("0.0.0.0", 8000))6Server.listen (5)7 Print("start to listen". Center (30,"-"))8 9 whileTrue:TenCONN,CLIENT_ADDR =server.accept () One Print(CONN,CLIENT_ADDR) A whileTrue: - Try: -data = CONN.RECV (1024) the Print("receive from client:", data) - if(Data.decode ()). StartsWith ("Get"): -Data_cmd =Data.decode () -Data_cmd_list = Data_cmd.split (" ") +file_name = data_cmd_list[-1] - conn.send (str (os.path.getsize (file_name)). Encode ()) +f = open (file_name,"RB") AF_data =F.readlines () at forLineinchF_data: - Conn.send (line) - elif(Data.decode ()). StartsWith ("put"): -Data_size = CONN.RECV (1024) - ifData_size.decode () = ="no file": - Continue in Print("The file ' s size is%sm"% round (int (Data_size.decode ())/1024/1024, 2)) -Data_list = (Data.decode ()). Split (" ") tofile_name = data_list[-1] +f = open (file_name,"WB") -Total_size =Int (Data_size.decode ()) theFile_size =0 * whileTrue: $DATA2 = CONN.RECV (1024)Panax Notoginseng f.write (data2) -File_size + =Len (data2) the ifFile_size = =total_size: + Break A f.close () the Print("File Upload done") + Else: -Res_obj = subprocess. Popen (data,shell=true,stdout=subprocess. Pipe,stderr=subprocess. PIPE) $res =Res_obj.stdout.read () $ conn.send (str (RES)). Encode ()) - Print("--res Len:", Len (res)) - conn.send (RES) the exceptConnectionreseterror as E: - Print(CLIENT_ADDR," is break")Wuyi Break the exceptFilenotfounderror as E: - Print("There is no such file!") WuConn.send (b"no file") - Continue
Client side:
1 ImportSocket2 ImportOS3Client =Socket.socket ()4Client.connect (("192.168.168.128", 8000))5 #client.connect (("localhost", 8000))6 7 whileTrue:8 Try:9msg = input (">>>>>"). Strip ()Ten ifLen (msg) = = 0:Continue One Client.send (Msg.encode ()) A ifMsg.startswith ("Get"): -data = CLIENT.RECV (1024) - ifData.decode () = ="no file": the Print("There is no such file!") - Continue - Else: - Print("The file ' s size is%sm"% round (int (Data.decode ())/1024/1024,2)) +Msg_list = Msg.split (" ") -file_name = msg_list[-1] +f = open (file_name,"WB") ATotal_size =Int (Data.decode ()) atFile_size =0 - whileTrue: -DATA2 = CLIENT.RECV (1024) - f.write (data2) -File_size + =Len (data2) - ifFile_size = =total_size: in Break - f.close () to elifMsg.startswith ("put"): +Msg_cmd_list = Msg.split (" ") -file_name = msg_cmd_list[-1] the client.send (str (os.path.getsize (file_name)). Encode ()) *f = open (file_name,"RB") $F_data =F.readlines ()Panax Notoginseng forLineinchF_data: - Client.send (line) the Else: +data = CLIENT.RECV (1024) A Print("Client Receive:", Data.decode ()) theTotal_size =Int (Data.decode ()) +Received_size =0 -res = b"" $ whileReceived_size <total_size: $D = CLIENT.RECV (1024) -Res + =D -Received_size + =Len (d) the Print("-----Receive done-----") - Print(Res.decode ())Wuyi exceptFilenotfounderror as E: the Print("There is no such file!") -Client.send (b"no file") Wu Continue
[Terry notes]python FTP