1. Execute cmd command
SERVER:__author__='Alex'#Coding:utf-8ImportSocketImportSubprocessip_port= ('127.0.0.1', 8888) SK=Socket.socket () sk.bind (Ip_port) Sk.listen (5)Print("Server start ...") whiletrue:conn,addr=sk.accept () whileTrue:Try: Recv_data= CONN.RECV (1024) exceptException: Break Print(Str (Recv_data,"UTF8")) cmd= STR (Recv_data,"UTF8"). Strip () Cmd_call= subprocess. Popen (cmd,shell=true,stdout=subprocess. PIPE)Print(Type (cmd_call)) Cmd_ret=Cmd_call.stdout.read ()Print(Type (cmd_ret))Print(Len (cmd_ret))ifLen (cmd_ret) = =0:cmd_ret= b'No output'conn.send (Cmd_ret) conn.close () CLIENT:__author__='Alex'#Coding:utf-8ImportSocketip_port= ('127.0.0.1', 8888) SK=Socket.socket () sk.connect (Ip_port)Print("client-initiated ...") whileTrue:send_data= Input (">>>>>"). Strip ()ifLen (send_data) = =0:Continue ifSend_data = ='Q': Breaksk.sendall (Bytes (Send_data,'GBK')) Recv_data= SK.RECV (1024) Print(Str (Recv_data,"GBK")) ifSend_data = ="Exit": Breaksk.close ()
Python cmd and sticky pack issues