Remote implementation of the CMD function:
ImportSocketImportSubprocessphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.bind (("127.0.0.1", 8080)) Phone.listen (5)Print("starting ....") whiletrue:conn,addr=phone.accept () whileTrue:Try: Date=CONN.RECV (1024) s=date.decode ("UTF8") Res=subprocess. Popen ("%s"% (s), shell=true,stdout=subprocess. Pipe,stderr=subprocess. PIPE,)ifRes.stdout:re1=Res.stdout.read () conn.send (Re1)ifRes.stderr:re2=Res.stderr.read () conn.send (Re2)exceptException: Breakconn.close () phone.close ( )
the server that actually executes the command
ImportSocketphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.connect (("127.0.0.1", 8080)) whiletrue:msg=input ("Please enter your cmd command:") if notMsgContinuephone.send (Msg.encode ("UTF8")) Data=PHONE.RECV (1024) Print(Data.decode ("GBK") ) Phone.close ()
Remote Control Client
What's wrong with the feeling? )
Python-based socket programming-------TCP-based sockets for remote execution of commands